java中的二维数组列表
所以这是我正在做的一个项目,需要我有一个一维数组的二维数组列表。但每次我尝试加载数据时都会收到错误:
由于输入错误,无法执行此操作 java.lang.IndexOutOfBoundsException:索引:1,大小:0
在某些输入上。我不知道我在这件事上哪里出了问题。请帮忙一点?
源代码:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.InputStream;
public class Facebull
{
public static void main (String[] args) {
if(args.length != 0){
load(args[0]);
}
else{
load("testFile");
}
}
public static void load(String fname) {
int costOfMach = 0;
ArrayList <Integer> finalMach = new ArrayList<Integer>();
ArrayList <ArrayList<int[]>>machines = new ArrayList<ArrayList<int[]>>();
Scanner inputFile = null;
File f = new File(fname);
if (f.exists ())
{
try
{
inputFile = new Scanner (f);
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"Can't find the file\n" + e);
}
int i = 0;
while (inputFile.hasNext ( ))
{
String str = inputFile.nextLine ( );
String [ ] fields = str.split ("[\t ]");
System.out.println(str);
if (!(fields[0].isEmpty() || fields[0].equals (""))){
fields[0] = fields[0].substring(1);
fields[1] = fields[1].substring(1);
fields[2] = fields[2].substring(1);
try
{
//data to be inputed is 0 and 3 location of data is 1 and 2
int[] item = new int[2];
item[1] = Integer.parseInt(fields[0]);
item[0] = Integer.parseInt(fields[3]);
if(machines.size() < Integer.parseInt(fields[1])){
ArrayList<int[]> column = new ArrayList<int[]>();
machines.add (Integer.parseInt(fields[1])-1, column);
System.out.println("we're in the if");
}
machines.get(Integer.parseInt(fields[1])-1).add(Integer.parseInt(fields[2])-1, item);
}
//catches any exception
catch (Exception e)
{
System.out.println("Can't do this opperation because of bad input \n" + e);
}
}
}
inputFile.close ( );
}
System.out.print(machines);
}//end load
}
So here's the deal I'm working on a project that requires me to have a 2 dimensional arraylist of 1 dimensional arrays. But every time I try to load in my data I get an error:
Can't do this opperation because of bad input
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
On some of the inputs. I've got no idea where I'm going wrong on this one. A little help please?
Source Code:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.InputStream;
public class Facebull
{
public static void main (String[] args) {
if(args.length != 0){
load(args[0]);
}
else{
load("testFile");
}
}
public static void load(String fname) {
int costOfMach = 0;
ArrayList <Integer> finalMach = new ArrayList<Integer>();
ArrayList <ArrayList<int[]>>machines = new ArrayList<ArrayList<int[]>>();
Scanner inputFile = null;
File f = new File(fname);
if (f.exists ())
{
try
{
inputFile = new Scanner (f);
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"Can't find the file\n" + e);
}
int i = 0;
while (inputFile.hasNext ( ))
{
String str = inputFile.nextLine ( );
String [ ] fields = str.split ("[\t ]");
System.out.println(str);
if (!(fields[0].isEmpty() || fields[0].equals (""))){
fields[0] = fields[0].substring(1);
fields[1] = fields[1].substring(1);
fields[2] = fields[2].substring(1);
try
{
//data to be inputed is 0 and 3 location of data is 1 and 2
int[] item = new int[2];
item[1] = Integer.parseInt(fields[0]);
item[0] = Integer.parseInt(fields[3]);
if(machines.size() < Integer.parseInt(fields[1])){
ArrayList<int[]> column = new ArrayList<int[]>();
machines.add (Integer.parseInt(fields[1])-1, column);
System.out.println("we're in the if");
}
machines.get(Integer.parseInt(fields[1])-1).add(Integer.parseInt(fields[2])-1, item);
}
//catches any exception
catch (Exception e)
{
System.out.println("Can't do this opperation because of bad input \n" + e);
}
}
}
inputFile.close ( );
}
System.out.print(machines);
}//end load
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里:
您还没有在任何地方指定机器 ArrayList 的大小。当您使用单参数版本的 add 时,ArrayList 将动态增长,因为它会添加到列表的末尾,但是当使用双参数版本时,ArrayList 必须至少与您指定的索引一样长。由于您没有指定大小,并且没有在任何地方使用单参数 add,因此 ArrayList 的大小为 0,因此无论您提供什么索引,它都会超出范围。您可以在创建 ArrayList 实例时指定大小,也可以在调用 add 之前使用 ArrayList.ensureCapacity。
然后,您在 get 行中遇到相同的问题,尝试在空 ArrayList 上使用两个参数添加。
Here:
You haven't specified a size for the machines ArrayList anywhere. ArrayList will dynamically grow when you use the one-argument version of add, since that adds to the end of the list, but when using the two-argument version, the ArrayList must already be at least as long as the index you're specifying. Since you haven't specified a size, and you haven't used the one-argument add anywhere, your ArrayList's size is 0, so no matter what index you provide, it'll be out of bounds. You can either specify a size when you create the ArrayList instance, or use ArrayList.ensureCapacity before calling add.
Then you have the same issue in the line with the get, trying to use two-argument add on an empty ArrayList.
如下所示为 2D ArrayList 声明 ArrayList:
现在在索引 0 处添加一个新的 String ArrayList:
现在,由于新的 ArrayList 在索引 0 处输入,因此您必须向该特定 ArrayList 添加值,如下所示:
Declare the ArrayList as shown below for a 2D ArrayList:
Now add a new ArrayList of String at index 0:
Now as new ArrayList is entered at index 0 so you have to add values to that particular ArrayList as shown: