如何输入长度可能不同的数组?输入是空间分隔和结束时我按下输入在java中输入未知大小的数组
public class ArrayInput {
public static void main(String args[]){
ArrayList<Integer> al = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while(){//what condition to use here?
al.add(sc.nextInt());
}
}
}
一个例子是:
1 2 5 9 7 4 //press enter here to mark end of input
是否在同一行所有你的整数?如果是这样,只需从'Scanner'中读取一行即可。 –
你可以参考http://www.big4future.com/2015/09/fast-io-with-java-good-for-online.html它为字符串,整数等 – Ansu