You are here:

Java/ArrayList and Array comparison

Advertisement


Question
Hello;

I have a 2D array of integers:

  int[][] layers = {
     {1,1,1,97},{1,2,1,98},{1,3,1,99},
     {1,1,2,100},{1,2,2,101},{1,3,2,102},
     {1,1,3,103},{1,2,3,104},{1,3,3,105},
     {2,1,1,106},{2,2,1,107},{2,3,1,108},
     {2,1,2,109},{2,2,2,110},{2,3,2,111},
     {2,1,3,112},{2,2,3,113},{2,3,3,114},
     {3,1,1,115},{3,2,1,116},{3,3,1,117},
     {3,1,2,118},{3,2,2,119},{3,3,2,120},
     {3,1,3,121},{3,2,3,122},{3,3,3,32}
         };


and also i have and ArrayList:
[[2, 1, 3], [3, 1, 1], [1, 1, 2], [1, 1, 1], [1, 2, 2], [2, 1, 1], [1, 3, 2]]

Now i want to check the layers array if elements of ArrayList exists.Note: Only the first 3 elements of the layers should me compared not the fourth one.

Answer
There are various ways of doing this. A simple iteration is one such way:

ArrayList<int[]> elements = ...;

for(int[] element : elements) {
  for(int[] layer : layers) {
     if(element[0] == layer[0] && element[1] == layer[1] && element[2] == layer[2]) {
        //Code to indicate found element here
        break;
     }
  }
}

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.