English 中文(简体)
相比之下,在 Java,有2个达1个
原标题:Compare 2d array to 1d in Java
  • 时间:2011-11-04 21:29:31
  •  标签:
  • java

Guys I m a bit new in using java and i m seeking to written a program that willeck the 2d protocol if it includes the Value of 1d protocol. 第二个阵列就像一个编号清单,如果与第一组阵列相对应,将予以检查。

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

这是我的法典:

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{

      }
    }
  }
}
最佳回答

这样做的简单办法是利用阿雷娜类,把你的阵列变成一个名单,使用该名单。 所有方法,如:

  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");
      }
  }
问题回答

仅澄清上文“Banthar”的意见意味着:

Java变量名称可以由一位数位数开始。 它们只能从美元或美元面值开始。 在第1封信之后,你可以使用数字。

如果你使用一个阵列清单,1 就会比较容易,因为你可以使用阵列清单确定该数值是否列入清单。

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
    }
}

希望这一帮助!

也可以使用阿雷米。

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]) {
        ...
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签