gpt4 book ai didi

java - 将 ArrayLists 的引用传递给方法

转载 作者:行者123 更新时间:2023-12-04 06:51:31 26 4
gpt4 key购买 nike

这是整个程序:

public  class ListMerge
{
public static void main( String[] args)
{

Scanner input = new Scanner(System.in);

System.out.println ("Input length of arraylist 1:");
int n = input.nextInt();
ArrayList x = new ArrayList();
ArrayList y = new ArrayList();
for (int i = 0; i < n; i++)
{
System.out.println ("Input x[ " + i +"] :" );
x.add(new Integer(i));

}


System.out.println ("Input length of arraylist 2:");
int m = input.nextInt();


for (int i = 0; i < m; i++)
{
System.out.println ("Input y[ " + i +"] :" );
y.add(new Integer(i));

}


}
list int merge(ArrayList<int> x, ArrayList<int> y)
{
List<Integer> all = new ArrayList<Integer>();

all.addAll(x);
all.addAll(y);
System.out.println(all);

return all;
}

}

还告诉我如何调用函数合并?

最佳答案

你只需声明一个这样的方法:

List<YourObjectType> mergeArrays(ArrayList<YourObjectType> array1, ArrayList<YourObjectType> array2) {

List<YourObjectType> mergedArray = new ArrayList<YourObjectType>();

// perform merge

return mergedArray;
}

关于java - 将 ArrayLists 的引用传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com