Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据从一个arraylist共享到其他几个arraylist_Java_Arraylist - Fatal编程技术网

Java 如何将数据从一个arraylist共享到其他几个arraylist

Java 如何将数据从一个arraylist共享到其他几个arraylist,java,arraylist,Java,Arraylist,我有这样的arraylist: 第一个arraylist: [ID 1,名称aa,TestN 500,类型R,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2013,ID 2,名称bb,TestN 500,类型R,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2014…] 方法将元素添加到arraylist list.add(new Data((String) it.next(), record[j]));

我有这样的arraylist: 第一个arraylist:

[ID 1,名称aa,TestN 500,类型R,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2013,ID 2,名称bb,TestN 500,类型R,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2014…]

方法将元素添加到arraylist

list.add(new Data((String) it.next(), record[j]));
如何分割此列表并添加到另一个arraylist(diviade的条件是Type,列表的计数是类型出现的数量)

因此,如果我在arraylist中有3个exmaple中出现的类型: R型 T型 Y型

预期产出:

第一个数组列表:[ID 1,名称aaa,TestN 500,类型R,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2013]

第二个数组列表:[ID 2,名称aab,TestN 500,类型T,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2014]

第三个数组列表:[ID 3,名称abc,TestN 500,类型Y,TestF 500.123,TestD Fri Sep 06 00:00:00 CEST 2015]

谢谢你给我的建议,我的英语也很抱歉

--编辑

在这个类中,我有来自.xls文件的数据

Head-是类似“ID”、“Name”、“TestN”类型的字符串。。
obj-这里有一组对象,如“1”、“aa”、“500”、“R”

首先,我认为您应该更改存储数据的方式。您的
数据
类如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
目前,您为每个字段创建了一个新的
数据
对象,因此您的列表如下所示:

[Data("ID", 1), Data("Name", "aaa"), Data("TestN", 500), Data("Type", "R"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2013"),
 Data("ID", 2), Data("Name", "aab"), Data("TestN", 500), Data("Type", "T"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
请注意,字段之间的关系完全丢失。您如何知道
ID1
Type R
属于同一类?依赖列表中对象的位置不是一个好选项。此外,当您使用
Object
作为所有值的类型时,您将丢失类型信息

更好的设计是使用适当的类型并将与特定记录相关的所有信息存储在同一对象中,如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
我使用了
final
修饰符,因此以后不能更改字段,必须在构造函数中进行设置。如果您喜欢使用getter和setter,可以删除它

现在,每个列表元素都是一条记录,所有相关信息都存储在一起

[Data(1, "aaa", 500, "R", 500.123, "Fri Sep 06 00:00:00 CEST 2013"),
 Data(2, "aab", 500, "T", 500.123, "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
我们可以创建一个只检索与特定类型匹配的对象的方法。如果您使用的是Java 8,则可以使用:

静态列表过滤器ByType(列表、字符串类型){
return list.stream().filter(item->type.equals(item.type))
.collect(Collectors.toList());
}
你可以这样称呼它:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
List typeRData=filterByType(List,“R”);

并且只获取
类型
“R”
的元素。首先,我认为您应该更改存储数据的方式。您的
数据
类如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
目前,您为每个字段创建了一个新的
数据
对象,因此您的列表如下所示:

[Data("ID", 1), Data("Name", "aaa"), Data("TestN", 500), Data("Type", "R"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2013"),
 Data("ID", 2), Data("Name", "aab"), Data("TestN", 500), Data("Type", "T"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
请注意,字段之间的关系完全丢失。您如何知道
ID1
Type R
属于同一类?依赖列表中对象的位置不是一个好选项。此外,当您使用
Object
作为所有值的类型时,您将丢失类型信息

更好的设计是使用适当的类型并将与特定记录相关的所有信息存储在同一对象中,如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
我使用了
final
修饰符,因此以后不能更改字段,必须在构造函数中进行设置。如果您喜欢使用getter和setter,可以删除它

现在,每个列表元素都是一条记录,所有相关信息都存储在一起

[Data(1, "aaa", 500, "R", 500.123, "Fri Sep 06 00:00:00 CEST 2013"),
 Data(2, "aab", 500, "T", 500.123, "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
我们可以创建一个只检索与特定类型匹配的对象的方法。如果您使用的是Java 8,则可以使用:

静态列表过滤器ByType(列表、字符串类型){
return list.stream().filter(item->type.equals(item.type))
.collect(Collectors.toList());
}
你可以这样称呼它:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
List typeRData=filterByType(List,“R”);

并且只获取
类型
“R”
的元素。首先,我认为您应该更改存储数据的方式。您的
数据
类如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
目前,您为每个字段创建了一个新的
数据
对象,因此您的列表如下所示:

[Data("ID", 1), Data("Name", "aaa"), Data("TestN", 500), Data("Type", "R"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2013"),
 Data("ID", 2), Data("Name", "aab"), Data("TestN", 500), Data("Type", "T"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
请注意,字段之间的关系完全丢失。您如何知道
ID1
Type R
属于同一类?依赖列表中对象的位置不是一个好选项。此外,当您使用
Object
作为所有值的类型时,您将丢失类型信息

更好的设计是使用适当的类型并将与特定记录相关的所有信息存储在同一对象中,如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
我使用了
final
修饰符,因此以后不能更改字段,必须在构造函数中进行设置。如果您喜欢使用getter和setter,可以删除它

现在,每个列表元素都是一条记录,所有相关信息都存储在一起

[Data(1, "aaa", 500, "R", 500.123, "Fri Sep 06 00:00:00 CEST 2013"),
 Data(2, "aab", 500, "T", 500.123, "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
我们可以创建一个只检索与特定类型匹配的对象的方法。如果您使用的是Java 8,则可以使用:

静态列表过滤器ByType(列表、字符串类型){
return list.stream().filter(item->type.equals(item.type))
.collect(Collectors.toList());
}
你可以这样称呼它:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
List typeRData=filterByType(List,“R”);

并且只获取
类型
“R”
的元素。首先,我认为您应该更改存储数据的方式。您的
数据
类如下所示:

class Data {
    private String head; {getters & setters}
    private Object obj; {getters & setters}
}
class Data {
    final int id;
    final String name;
    final int testN;
    final String type;
    final float testF;
    // here you can use any other date type you like
    final LocalDateTime testD; 

    Data(int id, String name, int testN, String type, float testF, LocalDateTime testD) {
        this.id = id;
        this.name = name;
        this.testN = testN;
        this.type = type;
        this.testF = testF;
        this.testD = testD;
    }
 }
List<Data> typeRData = filterByType(list, "R");
目前,您为每个字段创建了一个新的
数据
对象,因此您的列表如下所示:

[Data("ID", 1), Data("Name", "aaa"), Data("TestN", 500), Data("Type", "R"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2013"),
 Data("ID", 2), Data("Name", "aab"), Data("TestN", 500), Data("Type", "T"), Data("TestF", 500.123), Data("TestD", "Fri Sep 06 00:00:00 CEST 2014"),
 ...]
请注意,字段之间的关系完全丢失。您如何知道
ID1
Type R
属于同一类?依赖于对象在中的位置