sorting - 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 simulate 2-dimensional table(xy),function addelement performs insertion "table". can explain me why gives me error ?
exception in thread "main" java.lang.indexoutofboundsexception: index: 0, size: 0 @ java.util.arraylist.rangecheck(unknown source) @ java.util.arraylist.get(unknown source) @ pt.iul.poo.games.table.addelement(table.java:27)
your problem in line:
singleraw.addall(table.get(raw)); ^^^^^^ the exception you're getting informative:
exception in thread "main" java.lang.indexoutofboundsexception: index: 0, size: 0 it's telling list of size 0, , you're trying table.get(raw)); raw 0, if table of size 0, can't element @ 0. have list it's empty, didn't insert it.
you should change if(!table.equals(null)) if(table != null), because if table null, throw npe since it'll evaluated !null.equals(null)
also, try follow java naming conventions , change table table , class table.
Comments
Post a Comment