如何在java中的ArrayList中存储指定数量的整数序列?

如何在java中的ArrayList中存储指定数量的整数序列?,java,arraylist,Java,Arraylist,我想在ArrayList中存储指定数量的整数序列 序列的数量将由用户定义 例如,如果用户输入3,则程序将知道将插入3个序列 插入0时,每个序列结束。例如,下一个用户输入可能是: 1 2 3 0 4 5 6 7 8 0 11120 我想存储这些序列,然后对每个序列进行一些计算。我该怎么做 这就是我到目前为止所做的: public static void main (String[] args){ Scanner scan = new Scanner (System.in); Sys

我想在ArrayList中存储指定数量的整数序列

序列的数量将由用户定义

例如,如果用户输入3,则程序将知道将插入3个序列

插入0时,每个序列结束。例如,下一个用户输入可能是: 1 2 3 0 4 5 6 7 8 0 11120

我想存储这些序列,然后对每个序列进行一些计算。我该怎么做

这就是我到目前为止所做的:

public static void main (String[] args){
    Scanner scan = new Scanner (System.in);
    System.out.println("number of sequences: ");
    int size = scan.nextInt();
    List<Integer> arr = new ArrayList<Integer>();

    //How to get the input sequences by sequence?


    scan.close();
} 
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“序列数:”);
int size=scan.nextInt();
List arr=new ArrayList();
//如何按顺序获取输入序列?
scan.close();
} 

如果要使用ArrayList,可以使用泛型类型:

ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
ArrayList myList=new ArrayList();
您可以用整数数组列表填充此列表。 您可以使用for循环创建这个新的整数数组列表,当给定数量的列表出现时,它将退出循环。 例如:

for (int i=0; i < userInputNumber; i++) { 
    ArrayList<Integer> newList = new ArrayList<Integer>();
    // fill the newList object with the user input list of integers
    myList.add(newList)
}
for(inti=0;i
这里有一些半伪代码可以帮助您开始

//beginning number
int numOfSequences = readUserInputAsInt()

//make an array big enough to hold all the sequences
List<Integer>[] sequences = new List<Integer>[numOfSequences];

// read all the next input as one big string
String userInput = readUserInputAsString();

//this method would split the userInput string up by spaces to get each integer
int[] numbers = convertStringToIntArray(userInput);

//put each number in to the correct sequence list
int sequenceNumber = 0;
for(int number : numbers) {
    // if we haven't used this sequence before, create a new list to hold it
    if(sequences[sequenceNumber] == null){
        sequences[sequenceNumber] = new ArrayList<Integer>();
    }

    //  if number is 0, end that sequence, else add the number to the sequence
    if(number == 0) {
        sequenceNumber++;
    } else {
        sequences[sequenceNumber].add(number);
    }
}

// do work on 'sequences'
//起始编号
int numOfSequences=readUserInputAsInt()
//制作一个足以容纳所有序列的数组
列表[]序列=新列表[numOfSequences];
//将所有下一个输入作为一个大字符串读取
字符串userInput=readUserInputAsString();
//此方法将userInput字符串按空格拆分,以获得每个整数
int[]number=convertStringToIntArray(用户输入);
//将每个编号放入正确的顺序列表中
int sequenceNumber=0;
用于(整数:数字){
//如果我们以前没有使用过这个序列,请创建一个新列表来保存它
if(序列[sequenceNumber]==null){
sequences[sequenceNumber]=新的ArrayList();
}
//如果数字为0,则结束该序列,否则将数字添加到序列中
如果(数字==0){
sequenceNumber++;
}否则{
序列[sequenceNumber]。添加(编号);
}
}
//做“序列”的工作

你是怎么做的?确切地说是什么?你没有向我们展示到目前为止你做了什么,也没有提到你被困在了什么地方。您在读取用户输入时遇到问题吗?您不明白如何将输入解析为数字吗?您需要知道列表是如何工作的吗?公共静态void main(String[]args){Scanner scan=new Scanner(System.in);System.out.println(“数据输入”);int size=scan.nextInt();List arr=new ArrayList();//卡在这里-如何在“arr”中存储数字?scan.close();}请不要试图将代码转储到注释中。您需要编辑您的问题以将其添加到那里。注释中的代码未格式化且不可读。好的,只需编辑问题!