简体   繁体   English

Java 2D ArrayList

[英]Java 2D ArrayList

so every time i run the program and enter the 2nd choice, it tells me rangecheck error , index0 , size 0. 因此,每当我运行程序并输入第二个选择时,它就会告诉我rangecheck错误,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? 经过研究,我从中了解到arraylist为空,如何在2D arraylist中使用add函数?

|ABCDEFGHIJKLMNOPQRSTUVWX | ABCDEFGHIJKLMNOPQRSTUVWX

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

01|gggggggggggggggggggggggg 01 | gggggggggggggggggggggggg

02|gGGGGGGGGGGGGGGGGGGGGGGg 02 | gGGGGGGGGGGGGGGGGGGGGGGg

03|gGggggggggggggggggggggGg 03 | gGggggggggggggggggggggGg

04|gGgYYYYYYYYYYYYYYYYYYgGg 04 | gGgYYYYYYYYYYYYYYYYYYgGg

05|gGgYggggggggggggggggYgGg 05 | gGgYggggggggggggggggYgGg

06|gGgYggggggggggggggggYgGg 06 | gGgYggggggggggggggggYgGg

07|gGgYggYYYYYYYYYYYYggYgGg 07 | gGgYggYYYYYYYYYYYYggYgGg

08|gGgYggYggggggggggYggYgGg 08 | gGgYggYggggggggggYggYgGg

09|gGgYYYYggggggggggYYYYgGg 09 | gGgYYYYggggggggggYYYYgGg

10|gGggggggggggggggggggggGg 10 | gGggggggggggggggggggggGg

11|gGGGGGGGGGGGGGGGGGGGGGGg 11 | gGGGGGGGGGGGGGGGGGGGGGGg

12|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{
     }
    }
}

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. 现在,在情况2中,您尝试到达第16个数组的第一个元素(基本上是String类型),该元素为null,因为您尚未向该数组添加任何内容。 What you need to do before trying the get(index), is to check that the length of the array is bigger than the index. 在尝试使用get(index)之前,您需要做的是检查数组的长度是否大于索引。 In order to add to the array:content.get(16).add(str);. 为了添加到数组:content.get(16).add(str);。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM