源代码:
import java.util.Scanner;
public class addElement {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入需要的数组大小:");
Scanner scan=new Scanner(System.in);
int n = scan.nextInt();//接受输入的数组大小
int[]arr=new int[n];//定义一个大小为刚输入的n的数组
System.out.println("输入数组的每个元素:");
for(int i=0;i<arr.length;i++)
arr[i]=scan.nextInt();//依次输入元素到arr[i]
System.out.println("数组的元素依次为:");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+"\t");
}
}
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("input your number split by comma:");
String str = input.nextLine();
String arrStr[] = str.split(",");
int arrInt[] = new int[arrStr.length];
for(int i = 0;i<arrStr.length;i++) {
int a = Integer.parseInt(arrStr[i]);
arrInt[i] = a;
}
}
从控制台输入数字,用逗号隔开,可以直接放入数组当中
推荐于2017-09-01
for (int i = 0; i < array.length; i++) {
array[i] = new Object();
}
for(int i =0 ;i<array.length;i++){
array[i] = new Integer(i);
}