Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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 For正在覆盖arraylist_Java_Exception_Arraylist - Fatal编程技术网

Java For正在覆盖arraylist

Java For正在覆盖arraylist,java,exception,arraylist,Java,Exception,Arraylist,我正在做一个代码来读取3个名字,3个年龄,并打印谁是最年长的,谁是最年轻的。我正在将对象保存在arraylist中,但它似乎覆盖了集合,因此当我打印它时,字符串中只显示最后一个输入,同时显示最早的和最年轻的。有人能帮我吗 import java.util.Scanner; import java.util.ArrayList; public class Exercise2 { static class Person{ String name; int

我正在做一个代码来读取3个名字,3个年龄,并打印谁是最年长的,谁是最年轻的。我正在将对象保存在arraylist中,但它似乎覆盖了集合,因此当我打印它时,字符串中只显示最后一个输入,同时显示最早的和最年轻的。有人能帮我吗

import java.util.Scanner;
import java.util.ArrayList;

public class Exercise2 {

    static class Person{
        String name;
        int age;
        Scanner input = new Scanner(System.in);

        public void setName(){
            System.out.println("Input the name:");
            nome = input.next();
            input.nextLine();
        }

        public void setAge(){
            System.out.println("Input the age:");
            idade = input.nextInt();
            input.nextLine();
        }
    }

    static public void main(String[] args){
        ArrayList<Person> person = new ArrayList<Person>();
        Person p = new Person();
        Person aux = new Person();
        int i = 0;
        int j = 0;

        for(i = 0; i< 3; i++){
            p.setName();
            p.setAge();
            person.add(i,p);
            System.out.println( person.toString() );
            System.out.println( person.size() );
        }

        for(i = 0; i != 2; i++){
            for(j = 0; j != 2; j++){
                if(person.get(i).age > person.get(j).age){
                    aux.age = person.get(i).age;
                    person.get(i).age = pessoa.get(j).age;
                    person.get(j).age = aux.age;
                }
            }
        }
        System.out.println(person.get(i).name + " is the youngest and " + person.get(j).name + " is the oldest.");
    }
}
import java.util.Scanner;
导入java.util.ArrayList;
公开课练习2{
静态类人{
字符串名;
智力年龄;
扫描仪输入=新扫描仪(System.in);
public void setName(){
System.out.println(“输入名称:”);
nome=input.next();
input.nextLine();
}
公共无效设置(){
System.out.println(“输入年龄:”);
idade=input.nextInt();
input.nextLine();
}
}
静态公共void main(字符串[]args){
ArrayList person=新建ArrayList();
人员p=新人员();
人员辅助=新人员();
int i=0;
int j=0;
对于(i=0;i<3;i++){
p、 setName();
p、 设置();
增加(i,p);
System.out.println(person.toString());
System.out.println(person.size());
}
对于(i=0;i!=2;i++){
对于(j=0;j!=2;j++){
if(person.get(i.age)>person.get(j.age){
辅助年龄=个人获得(i)年龄;
person.get(i).age=pessoa.get(j).age;
person.get(j.age=aux.age;
}
}
}
System.out.println(person.get(i.name+)是最小的,“+person.get(j.name+”是最大的。”);
}
}

您正在创建一个
Person
实例,并多次将其添加到列表中。您应该每次在
for
循环中创建新实例,并添加到
列表中


您正在创建一个
Person
实例,并多次将其添加到列表中。您应该每次在
for
循环中创建新实例,并添加到
列表中


您正在创建一个
Person
实例,并多次将其添加到列表中。“For正在覆盖arraylist”,不,您将覆盖该列表中的值,而不是
For
循环;)。您正在创建一个
Person
实例,并多次将其添加到列表中。“For正在覆盖arraylist”,不,您将覆盖该列表中的值,而不是
For
循环;)。创建3个新的人员。因为它将只创建1个人员。@Apurva,for循环外部不会创建新实例。在整个列表中都是同一个人实例。创建3个新的人。因为它只会创建1个人。@Apurva,for循环外部不会创建新实例。在整个列表中,它将是同一个人的实例。
//Declare with List instead of ArrayList
List<Person> people = new ArrayList<Person>();

for(i = 0; i< 3; i++){
  Person p = new Person();// Move this line here.
  p.setName("Jon"); // Read attribute from file
  p.setAge(33);
  people.add(p);//Index should not mentioned
  ....
}
class Person{
    String name;
    int age;

    public void setName(String name){
      this.name=name;
    }

    public void setAge(int age){
      this.age=age.
    }
   }