我有一个数组列表的数组列表,我想创建一个单独的 View 来过滤其中的某些内容并将其存储在某个地方供以后使用。我计划使用数组列表的数组列表,想知道数组列表是否有一种方法可以连接在一起,其中一个的变化反射(reflect)另一个?有什么想法吗 ?它就像一张 Excel 工作表,我想过滤掉某些数字,我将显示同一张工作表的另一个 View ,而不会真正更改原始工作表。请帮忙。
最佳答案
这是一个完整的、可执行的演示程序。您应该能够按原样编译和运行。我没有添加评论或错误检查,但它应该按照你的要求去做。我不知道列表列表是否最适合您当前的情况。您的行同时具有名称和年龄这一事实让我认为您不需要列表列表,而是特定类的行对象列表。 (或者你可以在任何地方使用 List<?>
或 List<Object>
....)
不过,您可能会对以下内容感兴趣。您可能能够使用它,或者它可能会吓跑您远离列表方法。 :)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A ragged 2-D table of strings that supports "views" (arbitrary rows).
*
* WARNING: no error checking. This is just a sketch.
*/
public class Table {
private List<List<String>> data;
private List<Integer> activeRows = new ArrayList<Integer>();
public Table(List<List<String>> data) {
this.data = data;
}
public void setActiveRows(List<Integer> activeRows) {
this.activeRows = activeRows;
}
public void update(int row, int column, String value) {
data.get(row).set(column, value);
}
public void show() {
for (Integer row: activeRows) {
System.out.println(data.get(row));
}
}
/**
* DEMO. Normally we don't put main methods inside a class. This is just a sketch.
*/
public static void main(String[] args) {
List<List<String>> a = Arrays.asList(
Arrays.asList("abc", "def", "ghi"),
Arrays.asList("ABC", "DEF", "GHI"),
Arrays.asList("zzz", "yyy", "xxx"),
Arrays.asList("dog", "cat", "rat")
);
// Make two tables that share the same data
Table t1 = new Table(a);
Table t2 = new Table(a);
// t1 will have all four rows; t2 just two of them.
t1.setActiveRows(Arrays.asList(0, 1, 2, 3));
t2.setActiveRows(Arrays.asList(1, 3));
// Show them
t1.show();
System.out.println();
t2.show();
System.out.println();
// Now change part of t1 and show that this is reflected in t2.
t1.update(1, 2, "NEWVALUE");
// And show them again
t1.show();
System.out.println();
t2.show();
System.out.println();
}
}
输出是:
[abc, def, ghi]
[ABC, DEF, GHI]
[zzz, yyy, xxx]
[dog, cat, rat]
[ABC, DEF, GHI]
[dog, cat, rat]
[abc, def, ghi]
[ABC, DEF, NEWVALUE]
[zzz, yyy, xxx]
[dog, cat, rat]
[ABC, DEF, NEWVALUE]
[dog, cat, rat]
关于java - 将多个数组列表连接到一个数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7357411/