求助帖:牛客笔试数组输入问题
百度笔试和爱立信的笔试中的问题
在牛客笔试遇到两次这种情况了,输入一个数字和一行数字:
无论用哪种方式总是包数组非法越界的问题,求助这种怎么写输入呀????
我的方法:
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int n = Integer.parseInt(s);
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
} 用整行读取再split方法: Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int n = Integer.parseInt(s);
String s2 = sc.nextLine();
String[] str = s2.split(" ");
int[] arr = new int[str.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(str[i]);
} 都会报错数组越界,请问正确的写法是啥呀???我真菜!!!!
查看16道真题和解析