java 将任何类型的 ArrayList 传递给方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11734988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:11:31  来源:igfitidea点击:

Passing an ArrayList of any type to a method

javaandroidgenerics

提问by codingatty

I'm working on my first Android app, a math game for my kid, and am learning Java in the process. I have two ArrayLists, one of integers 0..9and one strings of possible operations, at present, just +and -.

我正在开发我的第一个 Android 应用程序,一个为我孩子准备的数学游戏,并在此过程中学习 Java。我有两个ArrayLists,一个整数0..9和一个可能的操作字符串,目前,只是+-

I would like to write a method that returns a random index into an ArrayList, so I can select a random element. What I'm running into is that I need two methods, one for each type of ArrayList, even though the code is identical. Is there a way to do this in a single method?

我想编写一个将随机索引返回到 的方法ArrayList,以便我可以选择一个随机元素。我遇到的是我需要两种方法,一种用于每种类型的ArrayList,即使代码是相同的。有没有办法在单一方法中做到这一点?

What I use now:

我现在用的:

Random randomGenerator = new Random();

  . . .

n = randomIndexInt(possibleOperands);
int op1 = possibleOperands.get(n);
n = randomIndexInt(possibleOperands);
int op2 = possibleOperands.get(n);
n = randomIndexStr(possibleOperations);
String operation = possibleOperations.get(n);

    . . .

int randomIndexInt(ArrayList<Integer> a){
    int n = randomGenerator.nextInt(a.size());
    return n;
}

int randomIndexStr(ArrayList<String> a){
    int n = randomGenerator.nextInt(a.size());
    return n;
}

What I'd like to do is collapse randomIndexIntand randomIndexStrinto a single method.

我想要做的就是崩溃randomIndexInt,并randomIndexStr成一个单一的方法。

回答by didxga

declare your method as int randomIndexInt(ArrayList<?> a)

将您的方法声明为 int randomIndexInt(ArrayList<?> a)

回答by Nermeen

You need only the size of the array right? so do it:

你只需要数组的大小对吗?这样做:

int randomIndex(int size){
    int n = randomGenerator.nextInt(size);
    return n;
}

回答by neworld

with this code you can pass any type of list

使用此代码,您可以传递任何类型的列表

int randomIndex(List<?> list){
    int n = randomGenerator.nextInt(list.size());
    return n;
}

回答by Priyank Doshi

More generic is to use Listthan ArrayListin method signature.

ListArrayList在方法签名中使用更通用。

int your_method(List<?> a){
//your code
}

回答by Anders Metnik

just make it:

只要做到:

private int randomIndex(int size){
return randomGenerator(size);
}

And then call them with randomIndex(yourArray.size());

然后打电话给他们 randomIndex(yourArray.size());

回答by sunil

you can use Genericsas follows

你可以使用Generics如下

declare your function as below

声明你的功能如下

private <T> int randomIndex(ArrayList<T> a){
    int n = randomGenerator.nextInt(a.size());
    return n;
}

now you can pass ArrayList<String>or ArrayList<Integer>to this function without any issue like this

现在你可以传递ArrayList<String>或传递ArrayList<Integer>到这个函数而没有像这样的任何问题

ArrayList<String> strList = new ArrayList<String>();
strList.add("on1");
System.out.println(randomIndex(strList));
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
System.out.println(randomIndex(intList));

回答by Shark

int randomIndex1(ArrayList<?> a)
{
   int n = randomGenerator.nextInt(a.size());
   return n;
}

int randomIndex2(int size)
{
   int n = randomGenerator.nextInt(size);
   return n;
}