如何避免在ArrayList中重复?

时间:2017-03-02 06:13:11

标签: java arraylist

  

我使用for循环将学生详细信息添加到ArrayList。当我提供第二个学生详细信息时,它会覆盖第一个数据。   学习班

import java.util.Scanner;
public class Stud {
    private int rollNumber, age;
    private String firstName, lastName, gender;
    public Stud(int rollNumber, String firstName, String lastName, int age, String gender) {

    }
    public void setRollnumber(int rollNumber) {
        this.rollNumber=rollNumber;
    }
    public int getRollnumber() {
        return rollNumber;
    }
    public void setFirstname(String firstName) {
        this.firstName=firstName;
    }
    public String getFirstname() {
        return firstName;
    }
    public void setLastname(String lastName) {
        this.lastName=lastName;
    }
    public String getLastname() {
        return lastName;
    }
    public void setAge(int age) {
        this.age=age;
    }
    public int getAge() {
        return age;
    }
    public void setGender(String gender) {
        this.gender=gender;
    }
    public String getGender() {
        return gender;
    }
}
  

将数据添加到ArrayList的主类。

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


public class MainClass {

    public static void main(String[] args) {
        ArrayList<Stud> al=new ArrayList<Stud>();
        System.out.println("Enter the details:");
        for(int i=1;i<=10;i++) {
        int rollNumber;
        String firstName;
        String lastName;
        int age;
        String gender;
        Scanner in=new Scanner(System.in);      
        rollNumber=in.nextInt();
        firstName=in.next();
        lastName=in.next();
        age=in.nextInt();
        gender=in.next();

        Stud s=new Stud(rollNumber, firstName, lastName, age, gender);

            al.add(s);

            for (Stud stud : al) {
                stud.setRollnumber(rollNumber);
                System.out.println(stud.getRollnumber());
            }
        }           

    }

}
  

输出:第二个数据[2,2]后的轧辊编号[1]的实际输出。   预期输出应为Roll Number:A1   名称:F1 L1   性别:男   年龄:11岁   体育赛事:Javelin

     

资格:否(因为Javelin对15岁及以上的学生开放)

卷号:A2 名称:F2 L2 性别女 年龄:14岁 体育赛事:100米跑

资格:是

卷号:A3 名称:F3 L3 性别:男 年龄:11岁 体育赛事:50米跑 资格:否(仅限10至12岁的女孩)

3 个答案:

答案 0 :(得分:1)

这应该有效:

public class MainClass {

   public static void main(String[] args) {
       Set<Stud> al=new HashSet<>(); // use a set
       Scanner in=new Scanner(System.in); // init scanner here!
       System.out.println("Enter the details:");
       for(int i=1;i<=10;i++) {
           int rollNumber;
           String firstName;
           String lastName;
           int age;
           String gender;

           rollNumber=in.nextInt();
           firstName=in.next();
           lastName=in.next();
           age=in.nextInt();
           gender=in.next();

           Stud s=new Stud(rollNumber, firstName, lastName, age, gender);

           al.add(s);

           for (Stud stud : al) {
               System.out.println(stud.getRollnumber());
           }
       }
   }

private static class Stud {
    private final int rollNumber, age;
    private final String firstName, lastName;
    private final String gender;

    // assign the values you get in constructor
    public Stud(int rollNumber, String firstName, String lastName, int age, String gender) {
        this.rollNumber = rollNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.gender = gender;
    }

    public int getRollnumber() {
        return this.rollNumber;
    }
    public String getFirstname() {
        return this.firstName;
    }
    public String getLastname() {
        return this.lastName;
    }
    public int getAge() {
        return this.rollNumber;
    }
    public String getGender() {
        return this.gender;
    }

    // needed for Set
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.age;
        result = prime * result + ((this.firstName == null) ? 0 : this.firstName.hashCode());
        result = prime * result + ((this.gender == null) ? 0 : this.gender.hashCode());
        result = prime * result + ((this.lastName == null) ? 0 : this.lastName.hashCode());
        result = prime * result + this.rollNumber;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Stud other = (Stud) obj;
        if (this.age != other.age)
            return false;
        if (this.firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!this.firstName.equals(other.firstName))
            return false;
        if (this.gender == null) {
            if (other.gender != null)
                return false;
        } else if (!this.gender.equals(other.gender))
            return false;
        if (this.lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!this.lastName.equals(other.lastName))
            return false;
        if (this.rollNumber != other.rollNumber)
            return false;
        return true;
        }
    }
}

答案 1 :(得分:1)

如果您能够在Java 8中使用Stream类,则可以使用distinct

list.stream().distinct().collect(Collectors.toList());

对象的'equals`方法至关重要。

答案 2 :(得分:0)

根据你的要求,希望这是有效的。覆盖以前的记录没有问题。使用Iterator类将记录添加到arraylist。

import java.util.*;
import java.util.ArrayList;
public class ArraLis {

    private int rollNumber, age;
    private String firstName, lastName, gender,event;
    ArraLis(int rollnum, String fname, String lname, int ag, String gend,String even) {
        rollNumber=rollnum;
        age=ag;
        gender=gend;
        firstName=fname;
        lastName=lname;
        event=even;
    }
    public void setRollnumber(int rollNumber) {
        this.rollNumber=rollNumber;
    }
    public int getRollnumber() {
        return rollNumber;
    }
    public void setFirstname(String firstName) {
        this.firstName=firstName;
    }
    public String getFirstname() {
        return firstName;
    }
    public void setLastname(String lastName) {
        this.lastName=lastName;
    }
    public String getLastname() {
        return lastName;
    }
    public void setAge(int age) {
        this.age=age;
    }
    public int getAge() {
        return age;
    }
    public void setGender(String gender) {
        this.gender=gender;
    }
    public String getGender() {
        return gender;
    }
public static void main(String args[])
{
    Scanner in=new Scanner(System.in);
    ArrayList<ArraLis> al=new ArrayList<ArraLis>();
    System.out.println("enter the details");
    int rollNumber;
    String firstName;
    String lastName;
    int age;
    String gender;
    String event;
    int i;
    for(i=0;i<=2;i++)
    {
    rollNumber=in.nextInt();
    firstName=in.next();
    lastName=in.next();
    age=in.nextInt();
    gender=in.next();
    event=in.next();


    ArraLis a1=new ArraLis(rollNumber, firstName, lastName, age, gender, event);
    al.add(a1);

    }
    Iterator it=al.iterator();
    while(it.hasNext())
    {
        ArraLis ar=(ArraLis)it.next();
        System.out.println(ar.rollNumber+" "+ar.firstName+" "+ar.lastName+" "+ar.age+" "+ar.gender+" "+ar.event);
    }



}
}