2016-06-17 36 views
-3

ここに私の問題があります: -配列名を配列に格納する方法は?

私は文字列として配列名(整数)を含む2つの文字列配列を持っています。 これで、各整数配列を残りの配列と比較する必要があります。

は、私は、配列

int[] data; 
    data1 = new int[] {10,20,30,40,50,60,71,80,90,91}; 
    data2 = new int[] {10,20,30,40,50,60,71,80,90,91}; 
    data3 = new int[] {10,20,30,40,50,60,71,80,90,91}; 
    data4 = new int[] {10,20,30,40,50,60,71,80,90,91}; 
    data5 = new int[] {10,20,30,40,50,60,71,80,90,91}; 

    /*Now divide the above data into two parts like data1 and data2 is in one side and other content is other side and each array will compare with other set of arrays. i.e. array data1 will compare with array data3, data4, data5. and same for data2 array */ 

    String [] str= new String[2]; 
    String [] str1= new String[3]; 

    str[0]="data"; //Name Of the first array. 
    str[1]="data2"; //This is also other array name 

    str1[0]="data3"; 
    str1[1]="data4"; 
    str1[2]="data5"; 
    //Now I need to call a function passing array name as the parameter. 

    for(int i=0;i<str.length();i++) 
    { 
    for(int j=0;j<str1.length();j++) 
     { 
     compare(str[i],str1[j]); //this is error part 


    //and the function definition is 
    public void compare(int[] x1, int[] x2) 
    {//compare code 
    } 

しかし、取得中にエラーがあるとします: - 互換性のない型:文字列は、あなたがint[]を受け入れる方法にStringを渡そうとしている[]

+1

マップを使用します。 – Walfrat

+0

私はあなたが 'compare(str [0])'の代わりに 'compare(data)'をやろうと思っています。 –

答えて

2

intに変換することはできません。アレイの名前を格納するための別のクラスを作成し、配列自体と比較する方法、そのクラスのオブジェクトを渡す

  1. : 私はここでは2つの方法を参照してください。 "名前" を格納するための
  2. 使用Map<String, Integer[]> - >int[]

輸入java.util.HashMapを。 import java.util.Map;

public class Main { 

    public static final String NAME_1 = "data"; 
    public static final String NAME_2 = "data2"; 

    public static void main(String[] args) { 
     Map<String, int[]> data = new HashMap<>(); 
     data.put(NAME_1, new int[]{10, 20, 30, 40, 50, 60, 71, 80, 90, 91}); 
     data.put(NAME_2, new int[]{10, 20, 30, 40, 50, 60, 71, 80, 90, 91}); 
     // etc... 
     compare(data.get(NAME_1)); 
    } 

    public static void compare(int[] x1) { 
     for (int i = 0; i < 10; i++) { 
      System.out.println(x1[i]); 
     } 
    } 

} 
+0

ちょっと、マップメソッドを使ってプログラミングを手伝ってもらえますか? –

+0

@ Ankur_009私はデータを挿入した後に私の答え – Andy

+0

を更新しました。配列名や配列を配列に格納する必要があります。次に内容を表示します。 –

関連する問題