Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA中如何在数组中存储数组名?_Java_Arrays_Function - Fatal编程技术网

在JAVA中如何在数组中存储数组名?

在JAVA中如何在数组中存储数组名?,java,arrays,function,Java,Arrays,Function,我的问题是:- 我有两个字符串数组,其中包含作为字符串的数组名(整数)。 现在我需要将每个整数数组与其余数组进行比较 假设我有一个数组 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};

我的问题是:-

我有两个字符串数组,其中包含作为字符串的数组名(整数)。 现在我需要将每个整数数组与其余数组进行比较

假设我有一个数组

    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[]数据;
数据1=新的整数[]{10,20,30,40,50,60,71,80,90,91};
数据2=新的整数[]{10,20,30,40,50,60,71,80,90,91};
数据3=新的整数[]{10,20,30,40,50,60,71,80,90,91};
数据4=新的整数[]{10,20,30,40,50,60,71,80,90,91};
data5=新的整数[]{10,20,30,40,50,60,71,80,90,91};
/*现在将上面的数据分成两部分,比如data1和data2在一边,另一部分内容在另一边,每个数组将与其他数组集进行比较。i、 e.阵列数据1将与阵列数据3、数据4、数据5进行比较。对于data2阵列也是如此*/
字符串[]str=新字符串[2];
字符串[]str1=新字符串[3];
str[0]=“数据”//第一个数组的名称。
str[1]=“数据2”//这也是另一个数组名
str1[0]=“数据3”;
str1[1]=“数据4”;
str1[2]=“数据5”;
//现在我需要调用一个传递数组名作为参数的函数。

对于(int i=0;i您试图将
String
传递给接受
int[]
的方法。 我在这里看到两种方式:

  • 创建单独的类来存储数组的名称、数组本身,并将该类的对象传递给compare方法
  • 使用
    Map
    存储“name”->
    int[]
  • 导入java.util.HashMap; 导入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]);
            }
        }
    
    }
    
    公共类主{
    公共静态最终字符串名称_1=“data”;
    公共静态最终字符串名称_2=“data2”;
    公共静态void main(字符串[]args){
    映射数据=新的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});
    //等等。。。
    比较(data.get(NAME_1));
    }
    公共静态无效比较(int[]x1){
    对于(int i=0;i<10;i++){
    系统输出println(x1[i]);
    }
    }
    }
    
    使用地图。我想你打算做
    比较(数据)
    而不是
    比较(str[0])
    。嘿,你能用map方法帮助编程吗。@Ankur\u 009插入数据后,我已更新了我的回答。我需要将数组名称或映射存储到数组中…然后显示内容..我为你的问题提供了解决方案。如果你想通过编码来解决你的特定问题,请提交赏金程序,否则接受a请回答。我更新了我的问题。提供的答案不是我要问的。请..@Andy。