强制ArrayIndexOutOfBoundException?

时间:2017-09-16 07:01:12

标签: java sockets indexoutofboundsexception

这是出于测试目的,因为我正在读一本书,而且我找不到练习中提到的程序。我需要创建一个ArrayIndexOutOfBoundException,以便可能调试和分析它。

应该像BufferOverFlow一样,例如在C中,可能不是Java的一个很好的例子。

程序似乎通过服务器执行从一个阵列分配给另一个阵列的内容。

在客户端上,我有一个包含10个有限内容的String-Array。服务器上的程序放了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);}
    }
}

3 个答案:

答案 0 :(得分:1)

  

在Java编程语言中,数组是对象(§4.3.1),是   动态创建,可以分配给Object类型的变量   (§4.3.2)。

Link

在这一行:

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

您要为sat分配长度为16的数组。您所做的简化版本:

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]);
    }
  }

答案 1 :(得分:0)

它可能并不像我想的那么好,但我更改了for循环并添加了一个新的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);
}

答案 2 :(得分:0)

尝试使用无效索引时会抛出此异常。

一个简单的

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

应该这样做。