Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

exception - Java Arraylist got java.lang.IndexOutOfBoundsException?

I'm a general 3D artist, switched from my career and started to learn programming. I've got a problem with c106a handout #5.

The code works, but I've still got some error log here.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at UniqueNames.showUnique(UniqueNames.java:23)
at UniqueNames.main(UniqueNames.java:39)

Why does Arraylist, which can stretch its capacity on its own, still get an OutOfBoundsException?

Here's my full code:

import acm.io.*;
import acm.program.ConsoleProgram;
import acm.util.*;
import java.io.*;
import java.util.ArrayList;
import java.lang.*;

public class UniqueNames extends ConsoleProgram{

  static ArrayList<String> meString = new ArrayList<String>();
  static String input ;

    public static void storeUnique(String input){
        if (!meString.contains(input))
           {
            meString.add(input);
            }
    }

    public static void showUnique(ArrayList<String> meString){
        System.out.println("Unique name list contains:");
        for(int i=0 ;i<= meString.size() ;i++){
            System.out.println(meString.get(i));
          }
      }

    public static void main(String[] args){

             try{
                InputStreamReader stream = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(stream);
                   while (true){
                       System.out.println("Enter name:");
                       String input = br.readLine();
                       if (input.equals("")) break;
                       storeUnique(input);
                      }  

                  {showUnique(meString);}  
                }
             catch(IOException e){
                }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following lines:

for (int i = 0; i <= meString.size(); i++) {
    System.out.println(meString.get(i));
}

should be:

for (int i = 0; i < meString.size(); i++) {
    System.out.println(meString.get(i));
}

This is because the index of the list starts from zero.

Index: 4, Size: 4 explains a little more. When you call get(4), an exception occurs because your list only has a size of 4. get(4) would attempt to access the 5th element in the list.

Valid elements you can access would be get(0), get(1), get(2), get(3).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...