0

so every time i run the program and enter the 2nd choice, it tells me rangecheck error , index0 , size 0.

what i understand from this after research is that the arraylist is empty, how do i use the add function in the 2D arraylist?

|ABCDEFGHIJKLMNOPQRSTUVWX

--+------------------------

01|gggggggggggggggggggggggg

02|gGGGGGGGGGGGGGGGGGGGGGGg

03|gGggggggggggggggggggggGg

04|gGgYYYYYYYYYYYYYYYYYYgGg

05|gGgYggggggggggggggggYgGg

06|gGgYggggggggggggggggYgGg

07|gGgYggYYYYYYYYYYYYggYgGg

08|gGgYggYggggggggggYggYgGg

09|gGgYYYYggggggggggYYYYgGg

10|gGggggggggggggggggggggGg

11|gGGGGGGGGGGGGGGGGGGGGGGg

12|gggggggggggggggggggggggg

package map;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;
import java.util.ArrayList;

public class MapMain 
{
    public static void main(String [ ] args)
    {
        Scanner input = new Scanner(System.in);
        InputStream is = null;
          int i;
          char c;
          String T;
          ArrayList<ArrayList<String>> Contain = new ArrayList<ArrayList<String>>();
          for(int L = 0; L < 30; L++)
          {
              Contain.add(new ArrayList<String>());
          }

          try{
              do{
            int a=0;
            String Elements;
            System.out.print("To load map enter 1\nTo print loded map enter 2\nTo change specific elements press 3  ");
            a=input.nextInt();
            switch (a){
            case 1 :
                System.out.print("What is the file dest?");
                T=input.nextLine();
                is = new FileInputStream(T);
                while((i=is.read())!=-1)
                {
                   c=(char)i;
                   String O = "ankosh";
                   //Contain.add(Contain.O);
                }
                break; 
            case 2:
                while(true)
                {
                   String U = Contain.get(16).get(0);
                   //System.out.print(Contain);
                   break;
                }
                break;
            case 3:
                System.out.print("What do you want to insert?");
                Elements=input.nextLine();
                //switch (Elements){
                //case 
                }
                break;
            } while(true);
          }catch(Exception e){

        // if any I/O error occurs
        e.printStackTrace();
     }finally{
     }
    }
}
4
  • first of all, always use camelCase for variable name Commented Oct 7, 2014 at 1:32
  • you need to get the first list and add on the second like Contain.get(0).add("this String"); Why are you using a double array list ? can't you use a map or something of that sort ? Commented Oct 7, 2014 at 1:39
  • @wrongAnswer could you provide an example or a source for a map? Commented Oct 7, 2014 at 1:54
  • @Ankosh please read on docs.oracle.com/javase/7/docs/api/java/util/Map.html & implementation docs.oracle.com/javase/7/docs/api/java/util/HashMap.html you should be able to find examples on the net. Commented Oct 7, 2014 at 2:00

1 Answer 1

1

You created the array of arrays, and the arrays it contains, so far it's ok. Now, on case 2 you are trying to reach the first element of the 16th array (basically of type String) which is null since you didn't add anything yet to this array. What you need to do before trying the get(index), is to check that the length of the array is bigger than the index. In order to add to the array:content.get(16).add(str);.

Sign up to request clarification or add additional context in comments.

8 Comments

Okay so i added the element and it worked thank you! had another question, i'm trying to load from a file into that ArrayList, so i want it in a table, now its like an array inside an array not an array[row][col] table, is it possible to have it in a table so the if the user wants , for example, to change Array[2][2] to input "Y"? because a lot of people told me to use HashMaps better @user265732
What do you mean by "is it possible to have it in a table so the if the user wants , for example, to change Array[2][2] to input "Y"?"
well the switch cases one of them is to change specif elements in the array depending on the row and col the user provides. how do i refer the row and col from the user to the index in the arraylist?
Well, if you use array of arrays, you can refer Array[2][2] as the third string of the third array: contain.get(2).set(2,"Y"); As I mentioned before, make sure that the index exists.
well the index would depend on how large the 2D array in the txt file actually is
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.