簡體   English   中英

如何將唯一值插入到 Java 中二維數組中的下一個空索引?

[英]How can I insert a unique value to the next empty index in a 2D array in 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("");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM