Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 在对象数组列表中搜索_Java_Search_Arraylist - Fatal编程技术网

Java 在对象数组列表中搜索

Java 在对象数组列表中搜索,java,search,arraylist,Java,Search,Arraylist,我的主要班级: public class favoriteshow { String name = null; int age = 0; String show = null; public Lab2(String name, int age, String show){ this.name = name; this.age = age; this.show = show; } public String toString(){ return "

我的主要班级:

public class favoriteshow {

String name = null; 
int age = 0; 
String show = null; 

public Lab2(String name, int age, String show){
    this.name = name; 
    this.age = age; 
    this.show = show; 
}

public String toString(){
    return "Name " + name + " age: " + age + " Favorite show: " + show; 
}

}
公共类NewClass{
公共静态void main(字符串参数[]){
int x=0;
ArrayList ArrayList=新的ArrayList();
而(x<5){
字符串名;
int年龄=0;
弦乐表演;
扫描仪=新的扫描仪(System.in);
System.out.print(“输入名称:”);
name=scanner.nextLine();
系统输出打印(“输入年龄:”;
年龄=scanner.nextInt();
系统输出打印(“输入显示:”);
show=scanner.nextLine();
Lab2 nub1=新的Lab2(姓名、年龄、显示);
arraylist.add(numb1);
x++;
}
System.out.println(arraylist);
}
}
(我知道我的while循环只会重复5次,因为我只是在测试它。)


因此,我有一个arraylist,用户在其中键入人名、年龄和最喜爱的节目。我想包括一个搜索功能,这样,如果我要求用户输入一个名字,它将显示所有同名的人的信息,而不仅仅是第一个人。我还想让它告诉我名单上的那个人是什么号码。例如,如果我有5个人,其中2个人有相同的名字,他们是2和4位,那么它会告诉我他们是第二和第四位

只需像在普通数组中一样迭代ArrayList索引,并与所需字段进行比较,例如:

public void searchAndShow(字符串名称){
对于(int i=0;i
只需像在普通数组中一样迭代ArrayList索引,并与所需字段进行比较,例如:

public void searchAndShow(字符串名称){
对于(int i=0;i
我会把这个放在我的主课上,还是放在我的favoriteshow课上?不客气。别忘了把我的答案标记为正确的。)我会把这个放在我的主课上,还是放在我的favoriteshow课上?不客气。别忘了把我的答案标记为正确的。;)你的课叫什么,favoriteshow还是Lab2?你似乎把名字弄混了。另外,如果是favoriteshow,使用大写字母(FavoriteShow)是很常规的。你的类叫什么,FavoriteShow还是Lab2?你似乎把名字弄混了。另外,如果是FavoriteShow,使用大写字母(FavoriteShow)也是很常规的。
public class NewClass {
public static void main(String args[]){
    int x = 0;

    ArrayList<favoriteshow> arraylist = new ArrayList<favoriteshow>();

 while( x < 5){ 
    String name;
    int age = 0;
    String show;

    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a name: ");
    name = scanner.nextLine();

    System.out.print("Enter an age: ");
    age = scanner.nextInt();

    System.out.print("Enter a show: ");
    show = scanner.nextLine();

    Lab2 nub1 = new Lab2 (name, age , show); 




    arraylist.add(nub1);
      x++;

    }

   System.out.println(arraylist);


 }
}