You don't need two loops to do this, One loop is sufficient to fix it
ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
int common=0;
for(int j=0;j<buttonresourceIds2.size();j++){
if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
System.out.println("Exist : "+buttonresourceIds2.get(j));
common++;
}else{
System.out.println("Not Exist : "+buttonresourceIds2.get(j));
}
}
return common;
Answer from PotatoManager on Stack OverflowYou don't need two loops to do this, One loop is sufficient to fix it
ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
int common=0;
for(int j=0;j<buttonresourceIds2.size();j++){
if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
System.out.println("Exist : "+buttonresourceIds2.get(j));
common++;
}else{
System.out.println("Not Exist : "+buttonresourceIds2.get(j));
}
}
return common;
See Below Code :
ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
for(int i=0;i<buttonresourceIds.size();i++){
if(buttonresourceIds2.contains(buttonresourceIds.get(i)))
{
System.out.println("Exist : "+buttonresourceIds.get(i));
}
else{
System.out.println("Not Exist : "+buttonresourceIds.get(i));
}
}
In Above code, only one Loop is required.
comparing elements of two arrayList in java - Stack Overflow
Comparing two arrays in java with for loop - Stack Overflow
Comparing two Arraylists
java - Compare elements from an ArrayList - Stack Overflow
Convert Lists to Collection and use removeAll
Collection<String> listOne = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
Collection<String> listTwo = new ArrayList(Arrays.asList("a","b", "d", "e", "f", "gg", "h"));
List<String> sourceList = new ArrayList<String>(listOne);
List<String> destinationList = new ArrayList<String>(listTwo);
sourceList.removeAll( listTwo );
destinationList.removeAll( listOne );
System.out.println( sourceList );
System.out.println( destinationList );
Output:
[c, g]
[gg, h]
[EDIT]
other way (more clear)
Collection<String> list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
List<String> sourceList = new ArrayList<String>(list);
List<String> destinationList = new ArrayList<String>(list);
list.add("boo");
list.remove("b");
sourceList.removeAll( list );
list.removeAll( destinationList );
System.out.println( sourceList );
System.out.println( list );
Output:
[b]
[boo]
This should check if two lists are equal, it does some basic checks first (i.e. nulls and lengths), then sorts and uses the collections.equals method to check if they are equal.
public boolean equalLists(List<String> a, List<String> b){
// Check for sizes and nulls
if (a == null && b == null) return true;
if ((a == null && b!= null) || (a != null && b== null) || (a.size() != b.size()))
{
return false;
}
// Sort and compare the two lists
Collections.sort(a);
Collections.sort(b);
return a.equals(b);
}
Only iterate on first arraylist with larger length and check for contains in second arraylist , if found set one else do nothing
for(int counter = 0; counter < firstList.size(); counter++) {
if(secondList.contains(firstList.get(counter))) {
comparingList.set(counter,1);
}
}
Whole java program
Just try to run the below program in http://www.compileonline.com/compile_java_online.php
import java.util.ArrayList;
import java.util.List;
public class CompareArrayListTest{
public static void main(String[] args) {
ArrayList<String> firstList = new ArrayList<String>();
firstList.add("book1");
firstList.add("book2");
firstList.add("book3");
firstList.add("book4");
ArrayList<String> secondList = new ArrayList<String>();
secondList.add("book1");
secondList.add("book2");
secondList.add("book3");
List<Integer> comparingList = new ArrayList<Integer>();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
comparingList.add(0);
}
for (int counter = 0; counter < firstList.size(); counter++) {
if (secondList.contains(firstList.get(counter))) {
comparingList.set(counter, 1);
}
}
System.out.println(comparingList);
}
BitSet bitset = new BitSet();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
comparingList.add(0);
}
for (int counter = 0; counter < firstList.size(); counter++) {
for (int counter2 = 0; counter < secondList.size(); counter++) {
if (secondList.get(counter2).equals(firstList.get(counter))) {
bitset.set(counter, 1);
}
}
}
You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.
Try this code.
char[] firstArray = {'a', 'b', 'c'};
char[] secondArray = {'a', 'b', 'c'};
if (firstArray.length != secondArray.length) {
System.out.println("False.they are not equal");
} else {
boolean isEqual = true;
for (int i = 0; i < firstArray.length; i++) {
if (firstArray[i] != secondArray[i]) {
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if (isEqual)
System.out.println("true.they are equal");
}
boolean isEqual = true;
for(int i=0;i<arrayLen;i++){
if(firstArray[i] != secondArray[i]){
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if(isEqual){
System.out.println("true.they are equal");
}
If that not work, try to change (firstArray[i] != secondArray[i]) to (!(firstArray[i].equals(secondArray[i]))) or st like that.
Can I sort two arraylists containing objects so that I can compare them and their contents? I tried to type in:
Collections.sort.myArrayList but it wouldnt work!
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
// compare list.get(i) and list.get(j)
}
}
What's the problem with using for loop inside, just like outside?
for (int j = i + 1; j < list.size(); ++j) {
...
}
In general, since Java 5, I used iterators only once or twice.
you should use CollectionUtils.retainAll : Returns a collection containing all the elements in collection1 that are also in collection2.
ArrayList commonList = CollectionUtils.retainAll(list1,list2);
You should transform you first list into a HashSet. HashSet lookups are O(1), and List lookups are O(n). This makes the whole algorithm O(n) rather than O(n^2)
Set<Foo> set1 = new HashSet<Foo>(list1);
for (Foo foo : list2) {
if (set1.contains(foo)) {
matchedPixels++;
}
}
It is explicitly clear what your intent is with the C-like for loops - loop over every pair exactly once, so that the same pair with reversed members, e.g. (a, b) and (b, a) doesn't get processed again. That is what I would recommend; it works in Java just as well.
However, if you absolutely have to have the enhanced for loop, you can have the inner for loop operate over a sublist, with List's subList method, starting with the next element.
for (Object o1 : list) {
List<Object> subList = list.subList(list.indexOf(o1) + 1, list.size());
for (Object o2 : subList) {
//Do something with o1 and o2
}
}
The enhanced for loop is not appropriate in all situations. If you avoid using an index, just to use indexOf in the loop, your code will be less efficient (indexOf is a linear search) and non-idiomatic.
In my opinion, the best answer is to use the explicit indices.
for (i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
// do something with list.get(i) and list.get(j)
}
}
One situation where you should avoid using get is if the List is a LinkedList, because get for a LinkedList is not a constant time operation. In this case I would do
List<Object> copy = new ArrayList<>(linkedList);
for (i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
// do something with copy.get(i) and copy.get(j)
}
}
I'm trying to do my homework
I created two ArraLists of Integer, and the content in the list is as follows:
List1:
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
List2:
150 300 450 600 750
when i use
list1. get(list1. size() - 1) == list2. get(0);
I get false;
I have to use
list1.get(list1.size() - 1).equals(list2.get(0));
to get true;
Then I try the following code:
ArrayList<Integer> arrList1 = new ArrayList<Integer>();
ArrayList<Integer> arrList2 = new ArrayList<Integer>();
arrList1. add(10);
arrList2.add(10);
System.out.println(arrList1.get(0) == arrList2.get(0));
I get true;
I know that == can only be used to compare values, or Primitive types, while .equals() is used to compare references.
I'm sure what I'm storing in the ArrayList is integer, int, so why can't I use == for comparison? (And I'm also wondering why comparing 10 with == gets true and 150 gets false)
I wonder why this is happening?
Thanks in advance to everyone who helped me.