【问题标题】:List<String[]> looping issueList<String[]> 循环问题
【发布时间】:2018-01-08 02:53:57
【问题描述】:

我在尝试向二维字符串矩阵添加额外的列时遇到重复问题。

这是我的代码:

List<String[]> rowValues;                      // the matrix, #of rows not  
                                               // important,  #of columns is 7]

String[] columnValues = new String[8];         // would contain the old row 
                                               // data plus one extra String

// LOOP ON ROWS
for(int i = 0; i < rowValues.size(); i++) {

    // LOOP ON COLUMNS
    for (int j = 0; j < rowValues.get(i).length; j++) {
        columnValues[j] = rowValues.get(i)[j];
    }

    columnValues[7] = "ENTRY" + i;
    rowValues.set(i, columnValues);

    System.out.println(rowValues.get(i)[0]);  // last element in each iteration
}
// END LOOPS

System.out.println(rowValues.get(0)[0]);      // element in 0-0 is 
                                              // the same as last row-0

我的问题是所有行都包含最后一行的数据,加上标记为的额外列: “输入”

例如,

[hi,    im,      haithem]
[this,  is,      hard]
[to,    figure,  out]

会的,

[to,   figure,  out,  ENTRY2]
[to,   figure,  out,  ENTRY2]
[to,   figure,  out,  ENTRY2]

【问题讨论】:

  • 不要将二维数组表示为List&lt;Foo[]&gt;。使用List&lt;List&lt;Foo&gt;&gt;
  • 或简称为Foo[][]
  • 数组列表不是一个好方法。
  • 你能添加想要的结果吗?
  • @ChristofferPass 我并不是说尺寸灵活。我的意思是,如果你使用List,你可以轻松地换掉实现:LinkedList、ArrayList、CopyOnWriteArrayList 等。除非有充分的理由使用它们,否则应避免使用数组。

标签: java for-loop duplicates


【解决方案1】:

您永远不会将columnValues 更改为指向新的String[],因此实际上rowValues 包含对同一对象的多个引用。

尝试将columnValues 的定义移动到外部for 循环内,这样每次迭代都会创建一个新的String[]

【讨论】:

  • 是的,这就是问题所在!谢谢。
【解决方案2】:

这里保存数据的是String[] 的实例。

您在这里只创建一个这样的实例:

String[] columnValues = new String[8];

因此,您将所有值写入同一个位置,您会看到 3 次相同的数组,因为您刚刚将 3 次相同的数组添加到列表中。

在内循环中的每一轮,您都会用新数据覆盖这个单个 columnValues 数组的内容,这会影响您已经放入列表中的内容,因为列表只是多次指向您设置的单个数组一遍又一遍地修改。

要解决您的问题,请将创建数组的行放在外循环内,以便为每一行分配一个新数组。

【讨论】:

  • 非常感谢!它是固定的。我花了一个小时盯着那个代码。
【解决方案3】:

您覆盖了columnValues 变量的值。

你应该在循环中声明它。

List<String[]> rowValues;                      // the matrix, #of rows not  
                                               // important,  #of columns is 7

//////////////////
// LOOP ON ROWS //
//////////////////

for(int i = 0; i < rowValues.size(); i++) {

    final String[] columnValues = new String[8];         // would contain the old row 
                                                         // data plus one extra String

    /////////////////////
    // LOOP ON COLUMNS //
    /////////////////////

    for (int j = 0; j < rowValues.get(i).length; j++) {
        columnValues[j] = rowValues.get(i)[j];
    }

    columnValues[7] = "ENTRY" + i;
    rowValues.set(i, columnValues);


    System.out.println(rowValues.get(i)[0]);  // last element in each iteration
}

///////////////
// END LOOPS //
///////////////

System.out.println(rowValues.get(0)[0]);      // element in 0-0 is 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2014-10-09
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2021-04-04
    相关资源
    最近更新 更多