Java数组列表比较
有谁能告诉我如何根据arrayList中每个组件的整数值列出arrayList中的一些数据吗?这是我的主课Java数组列表比较,java,sorting,arraylist,Java,Sorting,Arraylist,有谁能告诉我如何根据arrayList中每个组件的整数值列出arrayList中的一些数据吗?这是我的主课 import java.util.Scanner; import java.io.*; import java.util.Collections; import java.util.ArrayList; public class StudentDriver { public static void main(String[] args) throws IOException {
import java.util.Scanner;
import java.io.*;
import java.util.Collections;
import java.util.ArrayList;
public class StudentDriver {
public static void main(String[] args) throws IOException {
Scanner scan, urlScan, fileScan;
String url, file;
int count = 0;
scan = new Scanner(System.in);
System.out.println("Enter the name of the file");
fileScan = new Scanner(new File("Data.csv"));
ArrayList<Student> studentList = new ArrayList<Student>();
while(fileScan.hasNext()){
url = fileScan.nextLine();
urlScan = new Scanner(url);
urlScan.useDelimiter(",");
count++;
while(urlScan.hasNext()){
String name = urlScan.next();
String last = urlScan.next();
int score = urlScan.nextInt();
Student e = new Student(name,last, score);
studentList.add(e);
}
}
System.out.println("The file has data for" +count+ "instances");
int option;
do{
System.out.println("********");
System.out.println("Options:");
System.out.println("********\n1. List \n2. Add Student \n3.Delete Student \n4. Exit \n******** ");
System.out.print("Select option: ");
option = scan.nextInt();
if(option == 1){
int index = 0;
while(index<studentList.size()){
System.out.println(studentList.get(index));
index++;
}
}
else if(option == 2){
System.out.print("Enter the name of the student: ");
String newName = scan.next();
System.out.print("Enter the last name of the student: ");
String newLastName = scan.next();
System.out.print("Enter the exam score of the student: ");
int newScore = scan.nextInt();
Student b = new Student(newName, newLastName, newScore);
studentList.add(b);}
else if(option == 3){
System.out.print("Enter the name of the student to remove: ");
String remove = scan.next();
System.out.print("Enter the last name of the student: ");
String remove1 = scan.next();
int location = studentList.indexOf(remove);
location = studentList.indexOf(remove1);
studentList.remove(location);
}
}while(option!=4 && option <4);
}//main
}//class
import java.util.Scanner;
导入java.io.*;
导入java.util.Collections;
导入java.util.ArrayList;
公共班学生司机{
公共静态void main(字符串[]args)引发IOException{
扫描程序扫描、URL扫描、文件扫描;
字符串url,文件;
整数计数=0;
扫描=新扫描仪(System.in);
System.out.println(“输入文件名”);
fileScan=新扫描仪(新文件(“Data.csv”);
ArrayList studentList=新建ArrayList();
while(fileScan.hasNext()){
url=fileScan.nextLine();
urlScan=新扫描程序(url);
urlScan.useDelimiter(“,”);
计数++;
while(urlScan.hasNext()){
String name=urlScan.next();
字符串last=urlScan.next();
int score=urlScan.nextInt();
学生e=新学生(姓名、姓氏、分数);
学生名单。添加(e);
}
}
System.out.println(“该文件包含“+count+”实例的数据”);
int选项;
做{
System.out.println(“**********”);
System.out.println(“选项:”);
System.out.println(“*********\n1.List\n2.添加学生\n3.删除学生\n4.退出\n********”);
系统输出打印(“选择选项:”);
option=scan.nextInt();
如果(选项==1){
int指数=0;
而(index基本上你想要的是一个有序的集合。正如@duffymo所说的,考虑使用你的分数创建一个自定义比较器
关于从名单中删除学生,有很多信息。
studentList
是一个包含Student
对象的列表。
这意味着以下代码:
System.out.print("Enter the name of the student to remove: ");
String remove = scan.next();
System.out.print("Enter the last name of the student: ");
String remove1 = scan.next();
int location = studentList.indexOf(remove);
尝试查找名为的学生的索引。当您搜索字符串而不是学生
对象时,将返回-1
。
相反,您必须遍历studentList
,并将每个Student
元素的名字和姓氏与remove
和remove1
的值进行比较
for(Student student : studentList) {
if(student.getFirstName.equals(remove) && student.getLastName.equals(remove1)) {
// remove the student.
}
}
也可以考虑将每个<代码>学生<代码> ID作为唯一标识符。
尝试排序学生列表
Collections.sort(studentList, new Comparator<Student>()
{
@Override
public int compare(Student x, Student y)
{
if(x.score >= y.score)
return 1;
else
return -1;
}
});
Collections.sort(studentList,newcomparator()
{
@凌驾
公共整数比较(学生x、学生y)
{
如果(x.score>=y.score)
返回1;
其他的
返回-1;
}
});
您可以将compareTo方法更改为
public int compareTo(Student another)
{
if (this.score > another.score)
return -1;
if (this.score < another.score)
return 1;
else
return 0;
}
另外,如果您不想使用Collections.sort()方法,我可以向您展示如何在add选项下使用for循环编写它
Student newStd = new Student(name, last, score);
for(int i=0;studentList.size()>i;i++)
{
int size = studentList.size();
if(newStd.compareToCustom(studentList.get(i))>0)
{
studentList.add(i, newStd);
break;
}
else if(newStd.compareToCustom(studentList.get(size-1))<0)
{
studentList.add(studentList.size(), newStd);
break;
}
else if(newStd.compareToCustom(studentList.get(i))==0)
{
studentList.add(i++, newStd);
break;
}
}
Student newStd=新学生(姓名、姓氏、分数);
对于(int i=0;studentList.size()>i;i++)
{
int size=studentList.size();
if(newStd.compareToCustom(studentList.get(i))>0)
{
添加(i,newStd);
打破
}
else if(newStd.compareToCustom(studentList.get(size-1))考虑一个比较器。如果您需要关于我想做什么的更多信息,请告诉我。听起来您想要的是树集,而不是列表。我需要使用ArrayList(来自我的讲师)。我不知道TreeSet是如何工作的。btwI不需要更多的信息;您需要付出更多的努力。您将无法在遍历studentList时删除学生(并发修改例外)。然后保存索引或其他内容。这个想法是成立的。
Collections.sort(studentList)
Student newStd = new Student(name, last, score);
for(int i=0;studentList.size()>i;i++)
{
int size = studentList.size();
if(newStd.compareToCustom(studentList.get(i))>0)
{
studentList.add(i, newStd);
break;
}
else if(newStd.compareToCustom(studentList.get(size-1))<0)
{
studentList.add(studentList.size(), newStd);
break;
}
else if(newStd.compareToCustom(studentList.get(i))==0)
{
studentList.add(i++, newStd);
break;
}
}
else if ( option == 3)
{
System.out.print("Enter the first name of student will be deleted: ");
String removeName = scan.next();
System.out.print("Enter the last name of student will be deleted: ");
String removeLastName = scan.next();
for ( int i = 0; i < studentList.size(); i++)
{
Student deleted = studentList.get(i);
if ( deleted.getFirstName().toLowerCase().equals(removeName.toLowerCase()) && deleted.getLastName().toLowerCase().equals(removeLastName.toLowerCase()))
{
studentList.remove(i);
System.out.println("The student has been deleted.");
break;
}
else
{
System.out.println("This student is not found");
break;
}
}
}