Java - 重写一个列表<List<String>>

Java - rewrite a List<List<String>>

我想用新值重写我的 List<List<String>>

我正在使用 TableView (JavaFX),当我对列重新排序时,DataList 应该 updated/rewritten.

这是带有数据的 Table 的样子(例如这里我想将第一列与第二列交换):

在此示例中,我希望 1. 列具有第二列的数据,而 2. 列具有第一列的数据...

我写了这段代码,但它不起作用:

private ObservableList<List<String>> fnlData;
.
.
tmpListData = new LinkedList<List<String>>();
tmpListData.addAll(fnlData);
int i = 0;
for (List<String> ls : fnlData){
    int j = 0;
    for (String s : ls){
         s = tmpListData.get(i).get(colOrder[j]);
         j++;
    }
    i++;
}

我收到此错误:

当我第一次移动列时,没有任何反应,但新的列顺序号是正确的。 第二次对列重新排序,出现这个错误,列序号完全是假的...

小建议:

外部列表是行,内部列表是列

首先列的顺序是 = 0,1,2,3,4...

然后当我对列重新排序时,我将新顺序保存在 colOrder(//=5,0,1,2,3,4)

什么不起作用?是不是目录变了?还是视图未更新?

如果我假设外部列表表示行,内部列表表示列值,并且 colOrder 是一个包含类似 colOrder[0] = 1 的整数列表,这意味着第一列(数字0) 显示第 1 列的值,我会有这样的东西:

private ObservableList<List<String>> fnlData;

tmpListData = new LinkedList<List<String>>();

for (List<String> row : fnlData){
    List<String> newRow = new LinkedList<>();
    for (int col : colOrder){
        String value = row.get(col);
        newRow.add(value);
    }
    tmpListData.add(newRow);
}

根据评论更新

s 从未使用过,s 仅在 (!) for 循环中有效,但不会更改 ls 中的值。 要更改它:

...
for (List<String> ls : fnlData){    
    for (int j=0;j<ls.size();j++){
        ls.set(j, tmpListData.get(i).get(colOrder[j]));            
    }
    i++;
}

您确定要更改列表吗?您可以将您的列表包装在另一个 List 中,这实际上对元素进行了重新排序。

class ReorderedList<T> extends AbstractList<T> implements List<T> {

    final List<T> l;
    final int[] order;

    public ReorderedList(List<T> l) {
        this.l = l;
        order = new int[l.size()];
        // Start as original list.
        for (int i = 0; i < order.length; i++) {
            order[i] = i;
        }
    }

    @Override
    public T get(int index) {
        // Apply the reorder.
        return l.get(order[index]);
    }

    public void swap(int c1, int c2) {
        int temp = order[c1];
        order[c1] = order[c2];
        order[c2] = temp;
    }

    @Override
    public int size() {
        return l.size();
    }

}

public void test() {
    ReorderedList<String> s = new ReorderedList(Arrays.asList("One", "Two", "Three"));
    System.out.println(s);
    s.swap(0, 1);
    System.out.println(s);

}