2017-09-16 50 views
-1

這是用於測試的目的,因爲我正在讀一本書,而且我沒有找到練習中提到的程序。我需要創建一個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);} 
    } 
} 
+1

要獲得ArrayIndexOutOfBoundsException,您需要對數組,字符串和索引進行一些操作。據我所知,你不是。 –

+0

我使用帶16個字符串的sa []和sat []與10個限制數組。也許你可以指出應該在代碼中的哪個位置進行操作。謝謝 –

+1

'強制ArrayIndexOutOfBoundException?'與[tag:sockets]沒有任何關係。 – EJP

回答

1

在Java編程語言中,陣列是(第4.3.1節),是 動態創建的對象,並且可以是分配給Object (§4.3.2)類型的變量。

Link

在此行中:

sat = (String[]) stream.readObject(); 

您使用長度爲16的簡化版本,你做什麼分配給sat一個數組:

static void test1() throws Exception { 

    String sat[] = new String[10]; 
    //server 
    String[] sa = {"okay", "okay", "okay", "okay", "okay", "okay", "okay" 
     , "okay", "okay", "okay", "okay", "okay", "okay", "okay", "okay", "okay"}; 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ObjectOutputStream oos = new ObjectOutputStream(baos); 
    oos.writeObject(sa); 
    System.out.println(sat.length);//->10 
    //client 
    ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); 
    sat = (String[]) is.readObject();//->sat points to another array now 
    System.out.println(sat.length);//->16 

    } 

這將輸出10和16但不會像你期望的那樣拋出異常。

下面是示例代碼,將拋出ArrayIndexOutOfBoundsException

static void test() { 

    String words[] = new String[]{"Hello", "beautiful", "world!"}; 

    for (int idx = 0; idx <= words.length; idx++) { 
     System.out.println(words[idx]); 
    } 
    } 
0

這可能不太好,我想,但我改變了對循環並增加了一個新的String數組。現在它創建了例外。也許使用其他流會更好。

謝謝。

for(int i = 0; sat.length > i; i++) { 
s = sat[i]; 
sat2[i] = s; 
System.out.println("Array sat: " + sat.length); 
System.out.println("Array sat2: " + sat2.length); 
} 
0

當您嘗試使用無效索引時會拋出此異常。

簡單

int [] array = { 0 } ; 
array[1]++; 

應該做的。