から2DのArrayListを作成:2D配列リストに私はこのようになりますファイルから整数を取得しようとしているファイル
1;2;3;4;
5;6;7;8;
9;10;11;12;
。私はIndexOutOfBoundsException
を得続ける:
ArrayList<ArrayList<Integer>> myList = new ArrayList<>();
myList.add(new ArrayList<Integer>());
int i = 0;
int j = 0;
try
{
FileReader fin = new FileReader("data.txt");
Scanner scan = new Scanner(fin);
while(scan.hasNext())
{
if(scan.hasNextInt())
{
myList.get(j).add(scan.nextInt());
i++;
}
else if("\\n".equals(scan.next()))
{
myList.add(new ArrayList<Integer>());
j++;
}
else
{
System.out.println("Only use numbers!");
}
}
}catch(Exception e){}
System.out.println(myList.get(0).get(2));
ここでの問題は何ですか?
エラーがどこで発生しているか知っていますか? –
私はさらに: "IndexOutOfBoundsException:Index:2、Size:0"と書いています。プログラムが整数をarraylistsに追加していないと仮定して、arraylistsはどんな要求によっても境界から外れます。 – Sprawl63
おそらくあなたのファイルを見つけることはできませんが、あなたは例外を飲み込んでいます。 – bcarlso