在数组中查找重复对象

时间:2015-03-16 06:30:19

标签: java arrays object duplicates

我在试图找出如何在数组中找到重复的数字时遇到了麻烦。所以,基本上我必须弄清楚是否有重复的ID号。如果有,则重复的ID号将更改为最大ID号加1。但问题是,在数组中有整数和字符串,因为我从文本文件中获取它。

这是我的测试文件,其中包含我需要编码以查找重复项的最后一个方法:

public class TestStudent
{


public static void main(String[] args)
{

    boolean bool = true;

    try
    {
        File file = new File("c:/temp/student2.txt");
        Scanner input = new Scanner(file);
        Student studArray[] = new Student[100];
        int row = 0;

        while (input.hasNext())
        {
            String lName = input.next();
            String fName = input.next();
            String state = input.next();
            int studID = input.nextInt();
            int satScore = input.nextInt();
            studArray[row] = new Student(studID, lName, fName, state, satScore);
            row++;
            if (row == 5)
            {
                studArray[row] = new Student();    
            }
        }
        studArray[row+1] = new Student(777, "Kim", "Brian", "VA", 1300);
        findDuplicates(studArray);
        read(studArray);


    } 
    catch (FileNotFoundException e)
    {
        JOptionPane.showMessageDialog(null, "File not found!", "Error",
                JOptionPane.ERROR_MESSAGE);
    }
}
public static JTextArea read (Student studArray[])
{
    JTextArea outputOfArrays = new JTextArea();
    String text = "";
    text += "ID\tLast\tFirst\tState\tSAT score\n++++++\t"
            + "+++++++++\t+++++++++\t+++++++\t+++++++++++\n";
     for (int i = 0; i < studArray.length; i++)
    {
        if (studArray[i] != null)
        {
            text += studArray[i].StudentInfo() + "\n";
        }
    }
     outputOfArrays.append(text);
     JOptionPane.showMessageDialog(null, outputOfArrays, "Data", JOptionPane.INFORMATION_MESSAGE);
     return outputOfArrays;
}
**public static void findDuplicates(Student studArray[])
{   
    int max = 0;
    for(int i = 0; i < studArray.length; i++)
    {
        if (max < studArray[i].getStudID() && studArray[i].getStudID() < 1000)
            {
                max = studArray[i].getStudID();
            }
    }
    for(int i = 0; i < studArray.length; i++)
    {
        for(int j = 0; j < studArray.length; j++)
        {
            if(i != j && studArray[i].getStudID() == studArray[j].getStudID())
            {
                studArray[i].setStudID(max + 1);
                //max = studArray[i].getStudID() + 1;
            }
        }
    }
}**


}

这是我写的,但我真的觉得我做错了......

这是我在测试文件中使用的另一个java文件:

public class Student
{
   private int studID;
   private String fName;
   private String lName;
   private String state;
   private int satScore;


public Student()
{
    studID = 0;
    lName = null;
    fName = null;
    state = null;
    satScore = 0;
}
public Student(int a, String b, String c, String d, int e)
{
    setStudID(a);
    setLName(b);
    setFName(c);
    setState(d);
    setSatScore(e);
}
public String StudentInfo()
{
    String output = studID + "\t" + lName + "\t" + fName + "\t" + state + "\t" +
            satScore;
    return output;
}

public int getStudID() {return studID;}
public String getFName() {return fName;}
public String getLName() {return lName;}
public String getState() {return state;}
public int getSatScore() {return satScore;}


public void setStudID(int a)
{
    studID = (a > 99) ? a : 0; 
    studID = (a < 1000) ? a : 0; 
}
public void setLName(String b)
{
    if(b.length() > 2 && b.length() < 11)
    {
        lName = b;
    }
    else if(b.length() > 10)
    {
        lName = b.substring(0, 10);
    }
    else if(b.length() < 2)
    {
        lName = null;
    }
}
public void setFName(String c)
{
    if(c.length() > 2 && c.length() < 11)
    {
        fName = c;
    }
    else if(c.length() > 10)
    {
        fName = c.substring(0, 10);
    }
    else if(c.length() < 2)
    {
        fName = null;
    }
}
public void setState(String d)
{
    String abbrState[] = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", 
        "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA",
        "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC",
        "ND", "OH", "OK", "OR", "PA", "RO", "SC", "SD", "TN", "TX", "UT", "VT", 
        "VA", "WA", "WV", "WI", "WY"};
    for(int i = 0; i < abbrState.length; i++)
    {
        if(d.equals(abbrState[i]))
        {
            state = d;
        }
    }
}
public void setSatScore(int e)
{
    satScore = (e > 400) ? e : 0;
    satScore = (e < 1600) ? e : 0;
}

}

1 个答案:

答案 0 :(得分:0)

您应该修复此方法(以及setSatScore):

public void setStudID(int a){
    studID = (a <= 99) ? 0 : (a >= 1000) ? 0 : a; 
}

这是改进的方法:

public static void findDuplicates(Student studArray[]){   
    int max = 0;
    for(int i = 0; i < studArray.length; i++){
        if (max < studArray[i].getStudID() ){
            max = studArray[i].getStudID();
        }
    }
    Set<Integer> used = new HashSet<>();
    for(int i = 0; i < studArray.length; i++){
        int id = studArray[i].getStudID();
        if( used.contains( id ) ){
            studArray[i].setStudID(++max);   
        } else {
            used.add( id );
        }
    }
}

请注意,由于值超出范围而设置为0的ID不会更改。