Java 在数组中查找重复对象

Java 在数组中查找重复对象,java,arrays,object,duplicates,Java,Arrays,Object,Duplicates,我在想如何在数组中找到重复的数字时遇到了困难。所以,基本上我必须弄清楚是否有重复的身份证号码。如果有,则重复的ID号将更改为最大ID号加1。但问题是,数组中有整数和字符串,因为我是从文本文件中获取的 这是我的测试文件,其中包括我需要编码以查找重复项的最后一种方法: public class TestStudent { public static void main(String[] args) { boolean bool = true; try {

我在想如何在数组中找到重复的数字时遇到了困难。所以,基本上我必须弄清楚是否有重复的身份证号码。如果有,则重复的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;
            }
        }
    }
}**


}
公共类TestStudent
{
公共静态void main(字符串[]args)
{
布尔布尔布尔=真;
尝试
{
File File=新文件(“c:/temp/student2.txt”);
扫描仪输入=新扫描仪(文件);
学生studArray[]=新学生[100];
int行=0;
while(input.hasNext())
{
字符串lName=input.next();
字符串fName=input.next();
字符串状态=input.next();
int studID=input.nextInt();
int satScore=input.nextInt();
studArray[row]=新学生(studID、lName、fName、state、satScore);
行++;
如果(行==5)
{
studArray[row]=新学生();
}
}
studArray[row+1]=新学生(777,“Kim”,“Brian”,“VA”,1300);
findDuplicates(studArray);
读取(studArray);
} 
catch(filenotfounde异常)
{
showMessageDialog(null,“未找到文件!”,“错误”,
JOptionPane.ERROR\u消息);
}
}
公共静态JTextArea读取(学生studArray[]
{
JTextArea outputOfArrays=新的JTextArea();
字符串文本=”;
text+=“ID\tList\tFirst\tState\tSAT分数\n++++\t”
+“+t+t+t+t+t+t+t+t+t+t+t+t+n”;
for(int i=0;i
这是我写的,但我真的觉得我做错了

这是我在测试文件中使用的另一个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;
}
公共班级学生
{
私立国际学生会;
私有字符串fName;
私有字符串名称;
私有字符串状态;
私人智力测验成绩;
公立学生()
{
studID=0;
lName=null;
fName=null;
state=null;
SAT得分=0;
}
公立学生(整数a、整数b、整数c、整数d、整数e)
{
setStudID(a);
setLName(b);
setFName(c);
设定状态(d);
设定得分(e);
}
公共字符串StudentInfo()
{
字符串输出=studID+“\t”+lName+“\t”+fName+“\t”+state+“\t”+
satScore;
返回输出;
}
public int getStudID(){return studID;}
公共字符串getFName(){return fName;}
公共字符串getLName(){return lName;}
公共字符串getState(){return state;}
public int getSatScore(){return satScore;}
公共无效设置studid(int a)
{
studID=(a>99)?a:0;
studID=(a<1000)?a:0;
}
公共无效集合名称(字符串b)
{
如果(b.length()>2和&b.length()<11)
{
lName=b;
}
否则如果(b.长度()>10)
{
lName=b.子串(0,10);
}
否则如果(b.长度()<2)
{
lName=null;
}
}
公共void setFName(字符串c)
{
如果(c.length()>2和&c.length()<11)
{
fName=c;
}
否则如果(c.长度()>10)
{
fName=c.子串(0,10);
}
否则如果(c.长度()<2)
{
fName=null;
}
}
公共无效设置状态(字符串d)
{
字符串缩写[]={“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;i400)?e:0;
satScore=(e<1600)?e:0;
}

}

您应该修复此方法(以及设置ATSCORE):

public void setStudID(int a){
studID=(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 );
        }
    }
}
publicstaticvoidfindduplices(studArray[]){
int max=0;
for(int i=0;i

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

只有两个澄清:
1。如果是字符串,您希望忽略它们还是保持原样。
2。假设有两个重复的id,其中一个重复id是2,它位于3、6、8位置。最大id是50,所以在位置3,一切都很酷,但在6和8,它会变成51吗?没有“字符串”阿蒙
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 );
        }
    }
}