import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Stundent s1=new Stundent();
Stundent s2=new Stundent();
Stundent s3=new Stundent();
Stundent s4=new Stundent();
s1.setName("aa");
s1.setAge(12);
s2.setName("aa");
s2.setAge(13);
List<Stundent> list=new ArrayList<>();
List<Object[]> templist=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
list.add(s1);
list.add(s2);
Integer[] ageTotal = new Integer[1];
int index=0;
for(Stundent s:list){
int n=0;
if(!s.getName().isEmpty()){
n=s.getAge();
if(ageTotal[0]==null){
ageTotal[0]=n;
}
else{
ageTotal[0]+=n;
}
}
}
Object[] row = new Object[3];
row[0]="aa";
for (int j = 0; j < 1; j++) {
row[j+1] = ageTotal[j];
}
templist.add(row);
map.put("test",templist);
System.out.println(map.get("test"));
}
}
class Stundent{
private String name;
private int age;
public Stundent() {
}
public Stundent(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Java计算集合的列总和
最新推荐文章于 2024-07-06 02:48:37 发布