【新手求助算法】从一个List中整理出类的某个属性
一个List,比如说是一个List<Student>,每个Sutdent除了名字性别之外,还有所在的班级这个属性,怎么获取都有哪些班级呢
[解决办法]
#include <stdio.h>
int main()
{
int a[10] = {1,2,3,4,5,2,2,1,1,1};
int b[10], i;
for(i=0; i < 10; i++)
b[i] = 0;
// 将数据投射到数组b 中
for(i=0;i < 10; i++)
{
// 只计算一次 重复的无序计算
if(b[a[i]] == 0)
b[a[i]]++;
}
for(i=0; i < 10; i++)
{
// 输出非0 即存在的数字
if(b[i]!=0)
printf("%d", i);
}
return 0;
}
package study.suanfa;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import study.suanfa.entity.Student;
public class TestList {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
Student s1 = new Student("zhang", 1);
Student s2 = new Student("li", 1);
Student s3 = new Student("wang", 2);
Student s4 = new Student("zhao", 3);
Student s5 = new Student("qian", 2);
Student s6 = new Student("qq", 4);
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
students.add(s6);
Set<Integer> classIds = new HashSet<Integer>();
Iterator<Student> it = students.iterator();
while(it.hasNext()){
Integer i = it.next().getClassId();
System.out.println(i);
classIds.add(i);
}
System.out.println(classIds.toString());
}
}