1

Is there a way that I can compare an array and an arraylist like

if (array[0]==arraylist[0])

I am working on this problem:

String[] s= {"a","a","b","b","b","c"};
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
// create a new arraylist that stores the frequency of each character. 
// For example, "a" appears twice in s, so newArrayList[0]=2; "b" appears 
// three times, so newArrayList[1]=3, and so on.

My idea is using a loop to scan through the string array and every time when a letter in it equals the letter in list, int count++. But I dont know how to compare things in an array and things in an arraylist.

4
  • You are a bit vague about what the comparison should be. Is it just an equals? Or to you want to know something more specific. Commented Apr 5, 2015 at 2:53
  • You can't even compare two arrays that way (at least not meaningfully). Commented Apr 5, 2015 at 2:59
  • Why are you mixing arrays and array lists to begin with? It's certainly possible to compare the two together, but one would generally avoid mixing and matching their data structures in this manner. Commented Apr 5, 2015 at 3:19
  • Do you have to use an arraylist to store the frequency? Hashmap would be much better for this purpose Commented Apr 5, 2015 at 3:22

4 Answers 4

5

Shortest way is this

Arrays.asList(arr).equals(list)
Sign up to request clarification or add additional context in comments.

1 Comment

And not far off the fastest too
1

Yes, you can iterate through the array and arraylist and compare elements. ArrayList elements are accessed with .get(index). And if you are comparing Objects (non-primitives), you need to use the .equals() method, not ==.

if(array[i].equals(arrayList.get(i) ) )

Comments

0
import java.util.ArrayList;

public class ArrayAndArrayListElementsComparison {
    public static int compareArrayAndArrayListElements(String[] array, ArrayList<String> arraylist) {
        int count = 0;
        for (String s : array) {
            if (arraylist.contains(s)) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String[] array = {"a", "a", "b", "b", "b", "c"};
        ArrayList<String> arraylist = new ArrayList<String>();
        System.out.println("");
        arraylist.add("a");
        arraylist.add("b");
        arraylist.add("c");

        if (!arraylist.isEmpty() && array.length > 0) {
            if (compareArrayAndArrayListElements(array, arraylist) == array.length) {
                System.out.println("ArrayList contains all the elements in Array");
            } else if (compareArrayAndArrayListElements(array, arraylist) > 0) {
                System.out.println("ArrayList contains only few elements in Array");
            } else {
                System.out.println("ArrayList contains none of the elements in Array");
            }
        } else {
            System.out.println("Either ArrayList or Array is Empty.");
        }
    }
}

Comments

-1

You try

if (array[0].equals(arraylist[0]))

As in java comparisons string should use equals(). It's not like javascript.

1 Comment

arraylist[0] isn't going to work... Do you mean arraylist.get(0)?

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.