พอดีเป็นโจทย์ของน้อง แล้วผมก็เอามาดูแล้วงงเหมือนกัน มันรันใน jlab แล้วไม่ผ่านครับ ขึ้นว่า
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
อันนี้สคลิปครับ
public class SSS {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt(),y=in.nextInt(),h=0,s;
int a[] = new int[x];
int b[] = new int[x];
for (int i = 0; i < x; i++) {
a = in.nextInt(); }
Arrays.sort(a);
while (true) {
if (Arrays.binarySearch(a, y) < 0) break;
if (Arrays.binarySearch(a, y) >= 0) {
h++;
s = Arrays.binarySearch(a, y);
if (s != x-1){
System.arraycopy(a, s + 1, b, 0, x - s + 1);}
for (int i = 0; i < x; i++) {
a = b; }
}
}
System.out.println(h);
}
}
ผมลองแทนค่าแล้วไม่เข้าใจว่า arraycopy มัน OutOfBoundsException ตรงไหน
ช่วยหน่อยครับ ขอบคุณครับ
สอบถาม java เรื่อง arrays หน่อยครับ
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
อันนี้สคลิปครับ
public class SSS {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt(),y=in.nextInt(),h=0,s;
int a[] = new int[x];
int b[] = new int[x];
for (int i = 0; i < x; i++) {
a = in.nextInt(); }
Arrays.sort(a);
while (true) {
if (Arrays.binarySearch(a, y) < 0) break;
if (Arrays.binarySearch(a, y) >= 0) {
h++;
s = Arrays.binarySearch(a, y);
if (s != x-1){
System.arraycopy(a, s + 1, b, 0, x - s + 1);}
for (int i = 0; i < x; i++) {
a = b; }
}
}
System.out.println(h);
}
}
ผมลองแทนค่าแล้วไม่เข้าใจว่า arraycopy มัน OutOfBoundsException ตรงไหน
ช่วยหน่อยครับ ขอบคุณครับ