這是用於測試的目的,因爲我正在讀一本書,而且我沒有找到練習中提到的程序。我需要創建一個ArrayIndexOutOfBoundException,以便可能的調試和分析它。強制ArrayIndexOutOfBoundException?
應該像BufferOverFlow一樣,在C語言中,可能不是Java的一個好例子。
該程序似乎通過服務器執行從一個陣列分配給另一個陣列的內容。
在客戶端上,我有一個包含10個字符串數組的限制內容。來自服務器的程序放置了10多個字符串。
對不起,誤會。
爲什麼它有這種行爲?
客戶
import java.io.*;
import java.net.*;
import java.util.*;
public class JSimpleClient {
Socket sock;
Scanner eingabe;
String[] sat = new String[10];
BufferedWriter bw;
ObjectInputStream stream;
String s;
public static void main(String[] args) throws ClassNotFoundException {
JSimpleClient ct = new JSimpleClient();
ct.jetzt();
}
public void jetzt() throws ClassNotFoundException {
try {
sock = new Socket("127.0.0.1", 1000);
System.out.print("Bitte etwas eingeben:");
eingabe = new Scanner(System.in);
String input = eingabe.nextLine();
bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
bw.write(input);
bw.newLine();
bw.flush();
stream = new ObjectInputStream(sock.getInputStream());
sat = (String[]) stream.readObject();
for(String d : sat) {
s = d;
System.out.println(s);
}
} catch (ArrayIndexOutOfBoundsException ex) {System.out.println("BufferOverFlow ?!");}
catch(IOException ex) {ex.printStackTrace();}
}
}
服務器
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JsimpleServer {
String[] sa = {"okay","okay","okay","okay","okay","okay","okay"
,"okay","okay","okay","okay","okay","okay","okay","okay","okay"};
BufferedReader reader;
ObjectOutputStream oos;
ServerSocket ss;
Socket socket1;
boolean swi = true;
public static void main(String[] args) {
JsimpleServer jss = new JsimpleServer();
jss.startApp();
}
private void startApp() {
try {
ss = new ServerSocket(1000);
while(swi) {
socket1 = ss.accept();
System.out.println("Server is started !");
oos = new ObjectOutputStream(socket1.getOutputStream());
oos.writeObject(sa);
reader = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
System.out.println(reader.readLine());
oos.close();
}
} catch (IOException ex) {System.out.println("Couldn`t connect !");
Logger.getLogger(JsimpleServer.class.getName()).log(Level.SEVERE, null, ex);}
}
}
要獲得ArrayIndexOutOfBoundsException,您需要對數組,字符串和索引進行一些操作。據我所知,你不是。 –
我使用帶16個字符串的sa []和sat []與10個限制數組。也許你可以指出應該在代碼中的哪個位置進行操作。謝謝 –
'強制ArrayIndexOutOfBoundException?'與[tag:sockets]沒有任何關係。 – EJP