gpt4 book ai didi

java - 如何将扫描仪编号传递给 ArrayList

转载 作者:行者123 更新时间:2023-12-02 05:54:09 25 4
gpt4 key购买 nike

我的方法运行良好!我试图从用户那里获取信息,而不是像我在代码中那样自己输入数字。有没有一种方法可以让我传递数字而不将它们存储在不同的变量中!谢谢。

import java.util.*;

public class Interleave {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Welcome to Interleave program! ");
System.out.println();


System.out.print("how many number for array 1:= ");
int array1 = console.nextInt();
for (int i = 0; i < array1; i++) {
System.out.print("please enter nubmers := ");
int num = console.nextInt();
}

ArrayList<Integer> a1 = new ArrayList<Integer>();
a1.add(10);
a1.add(20);
a1.add(30);

System.out.println();
System.out.print("how many number for array 2:= ");
int array2 = console.nextInt();
for (int i = 0; i < array2; i++) {
System.out.print("please enter nubmers := ");
int num2 = console.nextInt();
}

ArrayList<Integer> a2 = new ArrayList<Integer>();
a2.add(4);
a2.add(5);
a2.add(6);
a2.add(7);
a2.add(8);

System.out.println();
System.out.println(a1);
System.out.println(a2);

interleave(a1, a2); //parameters

System.out.println(a1);


}//end of main
public static void interleave(ArrayList<Integer> list1, ArrayList<Integer> list2) {
int sizeList1 = list1.size();//size of array
int sizeList2 = list2.size();//size of array

ArrayList<Integer> temp = new ArrayList<Integer>(); //


for (int index = 0; index < list1.size(); index++)// copy elements ferom list 1
temp.add(index, list1.get(index));


for (int index = 0; index < list2.size(); index++)// copy elements ferom list 2
temp.add(index, list2.get(index));


int tempSize = temp.size(); //get temporary array size

temp.clear(); //clear tepm array


if (list1.size() > list2.size()) {
for (int index = 0, list1Index = 0, list2Index = 0;
index < sizeList1;
index = index + 2, list1Index++, list2Index++ )
{
temp.add(index, list1.get(list1Index));
temp.add(index + 1, list2.get(list2Index));
}


for (int index = 2 * list2.size(),
list2Count = list2.size();
index < tempSize; list2Count++, index++ )
temp.add(index, list1.get(list2Count));

list1.clear(); // clear list 1

for (int index = 0; index < temp.size(); index++)
list1.add(index, temp.get(index));
} else {
for (int index = 0, list1Index = 0, list2Index = 0;
index < sizeList2;
index = index + 2, list1Index++, list2Index++ )
{
temp.add(index, list1.get(list1Index));
temp.add(index + 1, list2.get(list2Index));
}

for (int index = 2 * list1.size(),
list1Count = list1.size(); index < tempSize;
list1Count++, index++ )
temp.add(index, list2.get(list1Count));


list1.clear();

for (int index = 0; index < temp.size(); index++)
list1.add(index, temp.get(index));
}


}//end of interleave


}//end of class

最佳答案

  1. 创建整数 ArrayList
  2. 每次需要添加号码时,请调用 mylist.add(console.nextInt())
  3. 完成后调用 mylist.clear(),或将其设置为新的 ArrayList。

请注意可能引发的各种异常并 try catch 它们,以便用户必须重新输入数字直到其有效。

关于java - 如何将扫描仪编号传递给 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23255225/

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