【问题标题】:Finding Duplicate Objects in Array查找数组中的重复对象
【发布时间】:2015-03-16 06:30:19
【问题描述】:

我在尝试找出如何在数组中查找重复数字时遇到了麻烦。所以,基本上我必须弄清楚是否有重复的身份证号码。如果有,那么重复的 ID 号将变为最大 ID 号加一。但问题是,数组中有整数和字符串,因为我是从文本文件中获取的。

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

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. 如果是字符串,您是要忽略它们还是保持原样。
    2. 假设有重复和一个这样的重复id 为 2,它位于 3、6、8 位置。 Max id 是 50,所以在位置 3,一切都很酷,但在 6 和 8,它会变成 51 吗?
  • ID 值中没有“字符串”。输入使用input.nextInt(),这要么从文本文件的数字序列中返回一个整数,要么抛出异常。
  • @Tarun 我想让字符串保持原样,例如,其中一个数组的输出将是 123,Johnson,David,1234。“123”是 studID。如果另一个数组具有相同的“123” studID 并且最大 ID 为“200”,则重复的 studID 将更改为“201”。

标签: java arrays object duplicates


【解决方案1】:

您应该修复此方法(以及 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 不会更改。

【讨论】:

  • 我似乎在 if(max
  • 如果您的输入循环没有读取所有 100 名学生的值,您将遇到麻烦。对象数组是变化无常的,除非完全填充。决定:扔掉数组并使用 ArrayList?除非你能读到 100,否则中止阅读?跟踪输入文件中的学生人数并使用该数字而不是数组长度?
猜你喜欢
  • 2018-08-13
  • 1970-01-01
  • 2018-11-21
  • 2022-01-24
  • 2016-04-17
  • 2018-12-18
  • 2014-08-08
  • 1970-01-01
  • 2017-02-02
相关资源
最近更新 更多