1

I am trying to compare input with values in an arraylist.

public compare(number)

Ex; I have an arraylist:

[100,1102,14051,1024, / 142,1450,15121,1482,/ 141,1912,14924,1001] // the / represents each entry

Each index of the array represents a unique attribute in my program. e.g index 0 represents user id, index 1 represents room number etc. If I do arraylist.get(2) it returns the second array (142,1450,15121,1482).

I'm trying to compare number to the 2nd element in each array. So say I run this compare(1102), I want it to iterate through each [1] in each array, and return true if there is a match at that index.

So I want it to compare the 'number' (1102) with each 1st index element (1102,1450,1912) and because 1102 is in the arraylist, return true

I've been searching around and couldn't find how to implement this, or word the question in the right way

3
  • 1
    what exactly are you stuck on? are you having trouble getting the list of the users from the main list? Commented Mar 1, 2019 at 14:17
  • I'm confused on how to access each element in the arraylist. If I get(0) it returns the whole first list. How can I access individual elements in the arraylist? Commented Mar 2, 2019 at 13:26
  • sorry for replying so late. Since arraylist.get(0) gives you an ArrayList, you can then use another get() to access the individual element (e.g. arraylist.get(0).get(0) will give you the first element of the first list) Commented Mar 4, 2019 at 14:05

3 Answers 3

1

The Stream API can accomplish this.

public class MyCompare 
{
    private static Optional<Integer> compare(Integer valueToCompare)
    {
        Optional<Integer[]> matchingArray = listToCompare.stream()
                .filter(eachArray -> eachArray[1].equals(valueToCompare))
                .findFirst();

        if(matchingArray.isPresent())
        {
            for(int i = 0; i < listToCompare.size(); i++)
            {
                if(listToCompare.get(i).equals(matchingArray.get()))
                {
                    return Optional.of(i);
                }
            }
        }

        return Optional.empty();
    }

    private static List<Integer[]> listToCompare = new ArrayList<>();

    public static void main(String[] args)
    {
        listToCompare.add(new Integer[] { 100, 1102, 14051, 1024 });
        listToCompare.add(new Integer[] { 142, 1450, 15121, 1482 });
        listToCompare.add(new Integer[] { 141, 1912, 14924, 1001 });

        Optional<Integer> listIndex = compare(1102);
        if(listIndex.isPresent())
        {
            System.out.println("Match found in list index " + listIndex.get());
        }
        else
        {
            System.out.println("No match found");
        }
    }
}

Match found in list index 0

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

Comments

0

The straight-forward way is to use an enhanced for-loop:

for (int[] items : list) {
    // do your checking in here
}

The somewhat more advanced, but much more elegant way would be to use streams:

list.stream()       // converts the list into a Stream.
    .flatMap(...)   // can map each array in the list to its nth element.
    .anyMatch(...); // returns true if any value in the stream matches the Predicate passed.

Stream API: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

Comments

0

Iterate over your list and compare every second element:

public static boolean compare (int number){
    for( int i = 0; i<arraylist.size(); i++){
         if(number == arraylist.get(i)[1]){
            return true;
          }
     }
    return false;
 }

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.