Reputation: 23
Guys I'm a bit new in using java and i'm trying to write a program that will check the 2d array if it contains the value of 1d array.The second array is like a list of numbers and it will check the first array if they match.
array1[6]= {"a","b","c","d","e","f"}
array2[1][4]={{"a","b","c","d"}{"d","e","f","g"}}
array2[0]= rowcomplete ; // because it contain all the value a,b,c,d
array2[1]= incomplete; // because it only match d,e,f but not g
This is my code:
String array1[] = {"a","b","c","d","e","f"};
String array2[][] = {{"a","b","c","d"}, {"d","e","f","g"}};
for (int 2row = 0; 2row < array2.length; 2row++) {
for (int 2column = 0;2column< array2[2row].length;2column++) {
for(int 1row=0; 1row < array1[1row].length();1row++) {
if (array2[2row][2column].equals(array1[1row])) {
System.out.println("complete");
}
else{
}
}
}
}
Upvotes: 2
Views: 1841
Reputation: 8139
You can also use Arrays.equals
String array1[] = {"a","b","c","d","e","f"};
String array2[][] = {{"a","b","c","d"}, {"d","e","f","g"}};
for (int i = 0; i < array2.length; i++) {
if(java.util.Arrays.equals(array1, array2[i]) {
...
}
}
Upvotes: 0
Reputation: 600
This would be easier if you use an array list for array1 because you can use the arrayList.contains() to determine if the value is in the list.
Using an array list and declaring/initializing array2 as you did before, try this:
ArrayList array1 = new ArrayList();
Collections.addAll(array1, "a", "b", "c", "d", "e", "f");
boolean matchFlag;
for(int i = 0;i< array2.length; i++){
matchFlag = true;
for(int j=0;j<array2[i].length; j++){
if(array1.contains(array2[i][j]) == false){
//found string that did not match
//
matchFlag = false;
}
}
if(matchFlag){
//complete array
}
}
Hope this helps!
Upvotes: 2
Reputation: 4324
Just to clarify what Banthar's comment above implies:
Java variable names can NOT start with a digit. They can only start with an _underscore, letter or a dollar sign $. After the first letter, you can use digits.
Upvotes: 2
Reputation: 6322
An easy way to do that would be to use the Arrays class to transform your arrays into a List and the use the containsAll method, like this:
String array1 []={"a","b","c","d","e","f"};
String array2 [][] ={{"a","b","c","d"},{"d","e","f","g"}};
List<String> array1AsList = Arrays.asList(array1);
for (int i = 0; i < array2.length; i++) {
List<String> array2rowAsList = Arrays.asList(array2[i]);
if(array1AsList.containsAll(array2rowAsList)){
System.out.println("row " + i + " is complete");
}
}
Upvotes: 3