Arrayindexoutofboundsexception là gì
File trr.txt chứa file Source của chương trình package BT2;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
class TRR2 {
public static void main(String args[]) {
File file = new File("D:\\test.txt");
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
int n = Integer.parseInt(line);
JOptionPane.showConfirmDialog(null, "Số lượng cặp đôi: \n" + line);
// Ma trận 1
String dong1 = br.readLine();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
String[] s = br.readLine().split(" ");
for (int j = 0; j < n; j++) {
int s_ = Integer.parseInt(s[j]);
a[i][j] = s_;
}
}
// ma trân 2
String dong2 = br.readLine();
int[][] b = new int[100][100];
for (int i = 0; i < n; i++) {
String[] s = br.readLine().split(" ");
for (int j = 0; j < n; j++) {
int s_ = Integer.parseInt(s[j]);
b[i][j] = s_;
}
}
// Ma trận 1
String xau1 = "";
for (int i = 0; i < n; i++) {
String xau2 = "";
for (int j = 0; j < n; j++) {
xau2 = xau2 + a[i][j] + " ";
}
xau1 = xau1 + xau2 + "\n";
}
JOptionPane.showConfirmDialog(null, "Yêu thích của Nam: \n" + xau1);
// Ma trận 2
String xau3 = "";
for (int i = 0; i < n; i++) {
String xau4 = "";
for (int j = 0; j < n; j++) {
xau4 = xau4 + b[i][j] + " ";
}
xau3 = xau3 + xau4 + "\n";
}
JOptionPane.showConfirmDialog(null, "Yêu thích của Nữ: \n" + xau3);
//Thuật toán kiểm tra
int s, w, t;
int[] next = new int[n];
int[] honphu = new int[n];
for (int i = 0; i < n; i++) {
next[i] = 0;
b[i][0] = n + 1;
honphu[i] = 0;
}
for (int m = 1; m < n; m++) {
s = m;
do {
next[s] = next[s] + 1;
w = a[s][next[s]];
if (b[w][s] <= b[w][honphu[w]]) {
t = honphu[w];
honphu[w] = s;
s = t;
}
} while (s == 0);
}
System.out.println("Ghep doi");
for (int i = 0; i < n; i++) {
System.out.println(i + " " + honphu[i]);
}
fr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} Sau khi chạy chương trình. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at BT2.TRR2.main(TRR2.java:78) Mảng trong file có kích thước 5. Lỗi ở câu lệnh if (b[w] <= b[w][honphu[w]]) { Mong các bạn chỉ giúp mình để có thể chạy chương trình được
Vì index của mảng đi từ 0 chứ không phải 1 nên nếu bạn để <= thì bị out of index
Chào bạn jackV mình để = 0 và
Trước dòng if (b[w] <= b[w][honphu[w]]) {
Tại sao không phải là b[m] mà là b[w] nhỉ, m mới là index trong vòng lặp mà
Khi thêm dòng System như bạn nói thì
W = 2
W = 5
W = 1
W = 4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at BT2.TRR2.main(TRR2.java:78)
đây là dòng khi mình in ra bạn ah
Đấy, mảng b có kích thước là 5, tức là chỉ số phần tử lớn nhất là 4. Nhưng lại xuất hiện cả w = 5. Ngược lại thì chỉ số 0 lại không có, mình nghĩ bạn nên để b[w - 1]
Chào bạn vậy thì phải làm sao hả bạn
Bạn Jack có thể leenYH mình nhờ tí được không?
hihi,
this part of program makes no sense to me: int s, w, t;
int[] next = new int[100];
int[] honphu = new int[100];
for (int i = 0; i < n; i++) {
next = 0;
b[0] = n + 1;
honphu = 0;
}
for (int m = 1; m < n; m++) {
s = m;
do {
next = next+1;
w = a[next];
if (b[w] <= b[w][honphu[w]]) {
t = honphu[w];
honphu[w] = s;
s = t;
}
} while (s ==0); 2) next = next+1; 3) w = a[next]; Could you please explain to me what you intend to do with these operations ? Reactions: SITUVN
hihi,
this part of program makes no sense to me:
Could you please explain to me what you intend to do with these operations ? I think it must throw Complie Error, but how he can run it without complie error?
Yes, me too.
Hi Hihi,
I need test.txt file for testing. Maybe I can help you to fix this bug. Please attach test file to the current thread.
I think Joe and SITUVN need to download the attached file to view original source code. May be there are some errors in the Forum, example [i . ] in the Java code will be understood a italic tag for the fragment content. YOU yourself should download the files he posted if you think you need test data. ________________________________________________ I always wonder when people try by all means to convert a to b without knowing that any conversion always causes friction (it's the law of physics). Even in Human Science. Translation of one-by-one or word-by-word between 2 languages is simply IMPOSSIBLE. I don't know what purpose this program is but what the link shows is either PASCAL or PL1.... here is the correction:
int s, w, t; int[] next = new int[n]; int[] honphu = new int[n]; for (int i = 0; i < n; i++) { next[i] = 0; b[i][0] = n + 1; honphu[i] = 0; } for (int m = 1; m < n; m++) { s = m; do { next[s] = next[s] + 1; if (next[s] < n){ w = a[s][next[s]]; if (w < n && b[w][s] <= b[w][honphu[w]]) { t = honphu[w]; honphu[w] = s; s = t; } else break; } } while (s == 0); } System.out.println("Ghep doi"); for (int i = 0; i < n; i++) { System.out.println(i + " " + honphu[i]); } and the output:
D:\Test>javac -g:none -d .\classes TRR.java D:\Test>java TRR Ghep doi 0 0 1 0 2 4 3 2 4 0 D:\Test> An other option: The data of test.txt shouldn't contain any value that's greater than the given max (here 5). If so the codes need not to be modified ! The rule is very simple: If I speak Java I think Java. If I speak Pascal I think Pascal... same for human language.
I usually don't download codes that contain Vietnamese characters because neither Notepad nor Jedit works with Vietnamese characters. Who wants help should be polite and prepares his/her codes so that people who want to help don't have to spend too much time for removing of such trivial problems. I'm not sure about your result. From http://katatunix.wordpress.com/2008/09/14/bai-toan-hon-nhan-bền-vững/ I see the result is :
Thuannd, I don't know what purpose this program is but what the link shows is either PASCAL or PL1....The rule is very simple: If I speak Java I think Java. If I speak Pascal I think Pascal... same for human language. |