2017-09-16 22 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]++; 

应该做的。