简体   繁体   English

在JAVA中模拟二维表的类

[英]class that simulates bi-dimensional table in JAVA

public class table 
{
private int raw=0;
private int column=0;
private List<ArrayList<Integer>> TABLE ;
private static int COUNT_ELEMENTS_IN_RAW=0;
private static int COUNT_ELEMENTS_TOTAL=0;
private List<Integer> singleRaw ;
public table()
{
    TABLE = new ArrayList<ArrayList<Integer>>();
    singleRaw = new ArrayList<Integer>();
}
public void addELEMENT(Integer value)
{   
    if(!TABLE.equals(null))
    {

        singleRaw.addAll(TABLE.get(raw));
        singleRaw.add(value);
        COUNT_ELEMENTS_IN_RAW++;
        if(COUNT_ELEMENTS_IN_RAW%14==0)
        {
            raw++;
            COUNT_ELEMENTS_IN_RAW=0;
            COUNT_ELEMENTS_TOTAL++;
        }
    }
}
}

here i´m trying to simulate 2-dimensional table(xy),function addELEMENT performs insertion into the "table". 在这里,我试图模拟二维表(xy),函数addELEMENT将插入到“表”中。 anybody can explain me why gives me that error ? 有人可以解释我为什么给我那个错误吗?

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at pt.iul.poo.games.table.addELEMENT(table.java:27)

Have you seen the collection type that Guava includes? 您是否看到过Guava包括的收藏类型? Table , which supports this use case for any "row" type and "column" type. Table ,支持任何“行”类型和“列”类型的此用例。

Table<Integer, Integer, String> table = HashBasedTable.create();
table.put(0, 0, "A"); // row 0, column 0
table.put(0, 1, "B"); // row 0, column 1

// One element
final String value = table.get(0, 1); // "B"

// Column 0
final Collection<String> strs = table.column(0).values();

// Row 1
final Collection<String> strs = table.row(1).values(); 

Your problem is in this line: 您的问题在这一行:

singleRaw.addAll(TABLE.get(raw));
                 ^^^^^^

The Exception you're getting is very informative: 您得到的异常非常有用:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

It's telling you that the List is of size 0, and you're trying to TABLE.get(raw)); 它告诉您List的大小为0,并且您正在尝试TABLE.get(raw)); where raw is 0, but if TABLE is of size 0, you can't get the element at 0. You do have an List but it's empty , you didn't insert anything to it. 其中raw为0,但如果TABLE的大小为0, 则无法将元素设置为0。您确实有一个List但它为 ,没有在其中插入任何内容。

You should also change if(!TABLE.equals(null)) to if(TABLE != null) , because if TABLE is null , this will throw a NPE since it'll be evaluated to !null.equals(null) 您还应该将if(!TABLE.equals(null))更改为if(TABLE != null) ,因为如果TABLEnull ,这将引发NPE,因为它将被评估为!null.equals(null)

Also, try to follow Java Naming Conventions and change TABLE to table and your class to Table . 另外,尝试遵循Java命名约定并将TABLE更改为table,并将类更改为Table

Look at line: 看一下行:

singleRaw.addAll(TABLE.get(raw));

There is the exception because of 由于有例外

TABLE.get(raw)

TABLE is empty so cannot get index 0 TABLE为空,因此无法获取索引0

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

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