简体   繁体   English

Java-调用具有不同变量类型的值的参数的方法

[英]Java - Invoking method with a parameter of the value of a different variable type

I have a bunch of variables declared as 2D arrays: 我有一堆声明为2D数组的变量:

int[][] array1 = {{}};
int[][] array2 = {{}};
etc..

I'm currently using a method to invoke another method with one of the variables as a parameter with a switch: 我目前正在使用一种方法来调用另一个方法,该方法将其中一个变量作为参数与开关一起使用:

public void invokeMethod(){
switch(variableNumber){
    case 1: method(piece1);
    break;
    case 2: method(piece2);
    break;
    etc..
  }
}

This feels like a phenomenally stupid way of approaching this issue, is there a more clever solution? 这似乎是解决问题的一种非常愚蠢的方式,是否有更聪明的解决方案? Perhaps storing the variables in an array? 也许将变量存储在数组中? What I'd like to do, in case I'm faced with thousands of 2D arrays, is invoking the method with the value of a variable of a different data type, like this: 万一遇到成千上万的2D数组,我想做的就是使用具有不同数据类型的变量的值来调用该方法,如下所示:

String arrayToBeInvokedWith = "array1";

method(arrayToBeInvokedWith){
}

(Yes, I do know this is too simplistic, but you can get the idea from it) (是的,我确实知道这太简单了,但是您可以从中得到启发)

Is there a similar solution? 有没有类似的解决方案? Any feedback is much appreciated. 任何反馈都非常感谢。

EDIT: All of the arrays have a same size. 编辑:所有的数组具有相同的大小。

Instead of declaring a bunch of individual arrays... 而不是声明一堆单独的数组...

Write a class that encapsulates an instance of a two dimensional array. 写一个封装二维数组实例的类。

If you then use a generic collection whose type is your special array class, you can put all the instances of your arrays into a single collection. 如果随后使用类型为特殊数组类的通用集合,则可以将数组的所有实例放入单个集合中。

Then when you need to operate on your arrays, you can just iterate over the collections. 然后,当您需要对数组进行操作时,可以仅遍历集合。

Or if it is important to have a particular identity associated with each of your 2D array objects, then associate the identity as the object's key in a HashMap. 或者,如果重要的是要与每个2D数组对象关联特定的标识,则将该标识作为HashMap中对象的键进行关联。 Then when you need to operate on whichever array, you can retrieve it from the map by using its key. 然后,当您需要对任何数组进行操作时,都可以使用其键从映射中检索它。

In place of 代替

public void invokeMethod(){
    switch(variableNumber){
        case 1: method(piece1);
            break;
        case 2: method(piece2);
            break;
        :
        :
    }
}

you can simply use something like 您可以简单地使用类似

public <T extends MyArrayClass> void invokeMethod(String arrKey, HashMap<String,T>
        arrays) {
    T my2dArray = arrays.get(arrKey);
    method(my2dArray);
    :
    :
}

When confronted with a problem like yours, the solution is often to create a new type that represents an abstraction for your data. 当遇到像您这样的问题时,解决方案通常是创建一个代表您的数据抽象的新类型。

You really need to use a array? 您真的需要使用数组吗? You can use a HashMap to store a key refering one array. 您可以使用HashMap来存储引用一个数组的键。 To get one array, you just pass the key of an array and get it. 要获得一个数组,只需传递一个数组的键即可。

HashMap<String, int[]> hashMap = new HashMap<String, int[]>();

int array1[] = {1, 2};
int array2[] = {2, 3};
int array3[] = {3, 4};

hashMap.put("array1", array1);
hashMap.put("array2", array2);
hashMap.put("array3", array3);

int arrayWanted[] = hashMap.get("array2");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java使用数组参数调用方法 - Java invoking a method with array parameter 调用具有不同参数类型的方法 - Invoking method with different argument type Java-具有通用类型参数并在调用它时进行类型转换的重写方法 - Java - Overriding method which has generic type parameter and type casting it while invoking it Java HashMap作为方法参数-未指定“值”类型 - Java HashMap as method parameter - not specifying the “value” type 未在形式参数类型中声明的对实际参数的调用方法 - Invoking method on actual parameter not declared in formal parameter type 在Kotlin中调用具有多个参数类型约束的泛型类型的方法 - Invoking a method with a generic type in Kotlin with multiple parameter type constraints Java参数名称不明确,尽管参数类型不同 - Java ambiguous method name, despite different parameter type 在不使用参数作为参数的情况下更改方法中的布尔变量的值(Java) - Changing value of a boolean variable within a method without using it as a parameter (Java) Java接口方法-参数和返回值类型为Number的子代 - Java interface method - parameter and return value type as child of Number 通用类型作为Java方法中的参数 - Generic type as parameter in Java Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM