1

I have two array list with name list and sum from this kind of class :

public class Factor {

    private String cat;
    private String kind;
    private String name;
    private int number;
    private String id;
}

my purpose is compare this two arraylist and if they have same object , list number = sum number else sum object add to list .

this is my try so far :

  int size=list.size();
  for (int j=0; j<size ;j++){
      for (int i = 0; i < sum.size(); i++) {
          if (list.get(j).getId().equals(sum.get(i).getId())){
              list.get(i).setNumber(sum.get(i).getNumber());
          } else {
              list.add(new Factor(sum.get(i).getId(),sum.get(i).getCat(),sum.get(i).getKind(), sum.get(i).getName(), sum.get(i).getNumber()));
          }
     }
 }

but problem is always two condition run any way it mean do below in if list.get(i).setNumber(sum.get(i).getNumber());

and after that do below in else

list.add(new Factor(sum.get(i).getId(),sum.get(i).getCat(), sum.get(i).getKind(),
                                                sum.get(i).getName(), sum.get(i).getNumber()));

always add list ... so where am i wrong ?

1
  • list.get(i).setNumber(sum.get(i).getNumber()); should be list.get(j).setNumber(sum.get(i).getNumber()); since you access list with j and sum with i. dont mix it up Commented Nov 30, 2017 at 9:58

3 Answers 3

1

Your logic was incorrect.

Based on the comments, you want to add to list all the elements of sum that don't have a matching ID in list. For that purpose you should iterate over the elements of sum first (i.e. in the outer loop).

int size=list.size();
for (int i = 0; i < sum.size(); i++) {
    boolean found = false;
    for (int j=0; j<size ;j++) {
        if (list.get(j).getId().equals(sum.get(i).getId())) {
            list.get(j).setNumber(sum.get(i).getNumber());
            found = true;
            break;
        }
    }
    if (!found) {
        list.add(new Factor(sum.get(i).getId(),sum.get(i).getCat(), sum.get(i).getKind(),
                                            sum.get(i).getName(), sum.get(i).getNumber()));

    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

list.get(i).setNumber(sum.get(i).getNumber()); is incorrect
seems your answer right but give (Invalid index 4, size is 4) error from this line list.add(new Factor(....) when want add to list
@Erf I didn't think it all the way though. When you want to add to the list, you should decide which element of the sum List to copy the data from. Choose any value between 0 and sum.size()-1 instead of i.
but i want add to list it sum which not find in list
@Erf But what if all the elements in the sum list don't have the same ID as list.get(j).getId(), what do you want to add to list? All of them? In that case you should probably reverse your loops (i.e. search for elements of sum that don't match any element of list)
|
0

you need to make sure both list size is the same, so if they are not the same size they wont be equal

Also this is a bad practice to compare two lists, a better way would be using a Set, just convert one of the lists to a set ( time complexity O(n) ) then loop over the other list and check if all elements are in the set you created from the other list, also you need to take care of duplicate case , so if duplicate is allowed in the list you need to use a map , where the id is the key and the value is the number of occurrences , while iterating over the other list if the key is found decrement the number and check if its not getting less than zero.

1 Comment

it would be same size or not! want my code handle all conditions
0

From your question, it's still not clear what you are trying to achieve from this code. Do you wanna compare every element of list array with every element of sum array or u just want to compare list array with the corresponding element of sum array.

As per my understanding,

From your code, I can see that u are using nested loos.

***for (int j=0; j<size ;j++)
          {
            for (int i = 0; i < sum.size(); i++) {}}***

So for every list(j) array, it will compare this all the elements of sum(i) array and out which some will execute IF block and some will execute else block depending upon the condition.

If this is not what u are looking for they give some more clarity on ur question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.