java - 如何在 Java 的二维数组中将唯一值插入到下一个空索引?
问题描述
我想让二维数组的每个索引在每次迭代中都有一个唯一的值,但我的问题是,每当用户输入第一个索引的第一个值时,它会自动将剩余的空索引覆盖到第一个索引值中......
String[][] ProductAllData1 = new String[10][6]; // an array that may store 10 unique elements(each element has 6 values)
String[] receivedPInputs = getPInputs(); // gets the values from a function that asks the user to input values
for (int d = 0; d < ProductAllData1.length; d++){
ProductAllData1[d] = receivedPInputs;
System.out.print(Arrays.toString(ProductAllData1[d]));
System.out.println("");
}
我是否缺少要添加的内容或者我的 for 循环不正确?
您的回复将不胜感激!!
解决方案
您正在为所有索引分配相同的值。
把getPInputs()
里面的循环!
String[][] ProductAllData1 = new String[10][6];
String[] receivedPInputs;
for (int d = 0; d < ProductAllData1.length; d++) {
receivedPInputs = getPInputs();
ProductAllData1[d] = receivedPInputs;
System.out.print(Arrays.toString(ProductAllData1[d]));
System.out.println("");
}
推荐阅读
- excel - Excel Vba如何避免重复迭代文件夹
- kubernetes-helm - Helm 图表标签和选择器
- sql - 查询插入行号以对结果进行分页
- javascript - 捕获设备位置时在导航器地理定位中找不到高度参数需要知道使用什么传感器来检测位置 GPS/Wifi
- list - 关闭 nats 列表上的引理
- python - 为什么我在使用 pip install 命令时会收到此错误
- yii2 - 在 yii2 中刷新浏览器后不会更改验证码
- spam - SPF 和 DMARC PASS,但域 XXX 的 DKIM“失败”?
- c# - 使用 puppeteer-sharp将文本抓取到一个类中
- c - MPLAB XC8 编译器错误:“声明中没有标识符”