简体   繁体   中英

existence of an element in two List

I want to verify the existence of an element in two JTable that displays questions The program is when I click on a question in the first list it will be transferred to the second JTable but I do not want to add duplication restrictions so I did not want an issue to be added twice in the second list I want make an added control if it will add the user can not add the second time

table = new JTable();
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            for (int i = 0; i < questions2.size(); i++) {
            /* the control code */  
                Question selected=questions.get(table.getSelectedRow());
                questions2.add(selected);
                questions.remove(selected);
                initDataBindings();
            } 


        }
    });

My Result

I do not want this

Use Set. Lets say you have a first List:

List<Question> questions1 = new ArrayList<Question>();

now, Question must have this methods:

@Override
public int hashCode() {
     // Let your IDE generates it based on variables
}

@Override
public boolean equals() {
     // Let your IDE generates it based on variables
}

With this method, Set will be able to figure out if two Question are equal , and create hashCode , needed for storing. Best way is if you have unique id , but other stuff can help to. If you add Question class to your Answer, I might help you decide the best implementation of equals() and hashCode() methods.

Now, you place them in HashSet , as Users does some action:

HashSet<Question> questionsSet = new HashSet<Question>();
boolean isPresentInList = questionsSet.contains(question);
questionsSet.add(question);

Method contains will check if that question is present in questionsSet . add will check if there is an equal question in it, defined by rules in equals() method, and if it is, it will override it, else it will just add. In this way, you will not have duplicates in questionsSet . Now, when you need an arrayList from questionsSet do this:

List<Question> questions2 = Arrays.asList(questionsSet);

since you are using Jtables in the question, you need to check if the value exists in the jtable first if the value doesnt exist then you can add it to the jtable. in the code you have submitted you are not checking if the value exists.

    public boolean ifExists(JTable table,string entry) {


    int rowCount = table.getRowCount();
    int columnCount = table.getColumnCount();


    for (int i = 0; i < rowCount; i++) {
        String value = "";
        for (int j = 0; j < columnCount; j++)
            value = value + " " + table.getValueAt(i, j).toString();
        if (value.equalsIgnoreCase(entry)) {
            return true;
        }
    }
    return false;
}

in the above code im checking the existence of the data using a for loop. it will iterate over the jtable and return true if the value exist. if the return value is false then you can go on and add the data. try it with the above code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM