Java Can';t访问数组的最后一个元素
我正在分析一个.CSV文件,一行接一行,我想得到列的值。 例如,我的.CSV文件:Java Can';t访问数组的最后一个元素,java,csv,multidimensional-array,indexoutofboundsexception,string-parsing,Java,Csv,Multidimensional Array,Indexoutofboundsexception,String Parsing,我正在分析一个.CSV文件,一行接一行,我想得到列的值。 例如,我的.CSV文件: time;columnA;columnB,ColumnC 27-08-2013 14:43:00; this is a text; this too; same here 因此,我所做的是将内容存储在二维字符串数组中(感谢split()。 我的数组如下所示: array[0][x] = "time". array[y][x] = "27-08-2013 14:43:00"; 它们是x个不同的列,但每个列的名称
time;columnA;columnB,ColumnC
27-08-2013 14:43:00; this is a text; this too; same here
因此,我所做的是将内容存储在二维字符串数组中(感谢split()。
我的数组如下所示:
array[0][x] = "time".
array[y][x] = "27-08-2013 14:43:00";
它们是x个不同的列,但每个列的名称仅存储在第[0][x]行。
它们是y个不同的行,其中的值存储为字符串
我的问题如下,我想获取数据的[x]位置,但当我尝试访问数组的最后[x]元素时。
我收到这个错误消息
java.lang.ArrayIndexOutOfBoundsException: 17
at IOControl.ReadCsvFile.getPosVar(ReadCsvFile.java:22)
at IOControl.ReadCsvFile.<init>(ReadCsvFile.java:121)
at en.window.Main.main(Main.java:48)
我想这可能是因为我没有检查x值是否小于完整字符串。
像这样:
x < index[x].length
x
但事实上,我没有改变任何东西,当我给出一个未知的字符串var
时,它也做得太过分了。
为什么?您完全忽略了数组边界。在while循环中,如何确保x变量不会太大?哪里都没有。您完全忽略了数组边界。在while循环中,如何确保x变量不会太大?没有 x<索引[x]。长度 问题不在于
索引[x]
的长度,而在于x
太大。您需要检查:
index.length < x
index.length
x<索引[x]。长度
问题不在于索引[x]
的长度,而在于x
太大。您需要检查:
index.length < x
index.length
在while循环结束时,增加x的值。然后,尝试在sysout方法中再次获取该值
编辑:尝试将x++放入else块。在while循环结束时,增加x的值。然后,尝试在sysout方法中再次获取该值
编辑:尝试将x++放入else块。在使用索引之前检查其有效性也是一个好主意:
if ( index == null || index.length == 0 ) return -1;
您的while循环应该更像这样:
while ( x < index[0].length )
{
if ( index[0][x] == null )
{
x++;
continue; // skip possible null entries.
}
if ( index[0][x].contains(var) )
{
System.out.println("x = " + x + ", val = " + index[0][x]);
return x; // return the position found.
}
x++;
}
return -1;
private int getPosVar(String[][] index, String var)
{
int x = 0;
boolean found = false;
if(index == null || index.length == 0 || var == null)
return -1;
while((x < index[0].length))
{
if (index[0][x].contains(var))
{
System.out.println("x = " +x+ " val = " +index[0][x]);
return(x);
}
x++;
}
System.out.println(" var = " + var + " not found");
return -1;
}
while(x
使用for循环(我更喜欢):
for(int x=0;x
在使用索引之前检查其有效性也是一个好主意:
if ( index == null || index.length == 0 ) return -1;
您的while循环应该更像这样:
while ( x < index[0].length )
{
if ( index[0][x] == null )
{
x++;
continue; // skip possible null entries.
}
if ( index[0][x].contains(var) )
{
System.out.println("x = " + x + ", val = " + index[0][x]);
return x; // return the position found.
}
x++;
}
return -1;
private int getPosVar(String[][] index, String var)
{
int x = 0;
boolean found = false;
if(index == null || index.length == 0 || var == null)
return -1;
while((x < index[0].length))
{
if (index[0][x].contains(var))
{
System.out.println("x = " +x+ " val = " +index[0][x]);
return(x);
}
x++;
}
System.out.println(" var = " + var + " not found");
return -1;
}
while(x
使用for循环(我更喜欢):
for(int x=0;x
您应该对照
x < index[0].length
在访问索引之前
找到正确的结果后,代码也会增加“x++”,因此x会进一步移动一个元素。
如果您现在找到最后一个元素或/或没有元素,那么这将设法溢出数组边界,因此
System.out.println("x = " +x+ " val = " +index[0][x]);
将抛出一个错误
我建议这样做:
while ( x < index[0].length )
{
if ( index[0][x] == null )
{
x++;
continue; // skip possible null entries.
}
if ( index[0][x].contains(var) )
{
System.out.println("x = " + x + ", val = " + index[0][x]);
return x; // return the position found.
}
x++;
}
return -1;
private int getPosVar(String[][] index, String var)
{
int x = 0;
boolean found = false;
if(index == null || index.length == 0 || var == null)
return -1;
while((x < index[0].length))
{
if (index[0][x].contains(var))
{
System.out.println("x = " +x+ " val = " +index[0][x]);
return(x);
}
x++;
}
System.out.println(" var = " + var + " not found");
return -1;
}
private int getPosVar(String[][]索引,String var)
{
int x=0;
布尔值=false;
if(index==null | | index.length==0 | | var==null)
返回-1;
而((x
您应该对照
x < index[0].length
在访问索引之前
找到正确的结果后,代码也会增加“x++”,因此x会进一步移动一个元素。
如果您现在找到最后一个元素或/或没有元素,那么这将设法溢出数组边界,因此
System.out.println("x = " +x+ " val = " +index[0][x]);
将抛出一个错误
我建议这样做:
while ( x < index[0].length )
{
if ( index[0][x] == null )
{
x++;
continue; // skip possible null entries.
}
if ( index[0][x].contains(var) )
{
System.out.println("x = " + x + ", val = " + index[0][x]);
return x; // return the position found.
}
x++;
}
return -1;
private int getPosVar(String[][] index, String var)
{
int x = 0;
boolean found = false;
if(index == null || index.length == 0 || var == null)
return -1;
while((x < index[0].length))
{
if (index[0][x].contains(var))
{
System.out.println("x = " +x+ " val = " +index[0][x]);
return(x);
}
x++;
}
System.out.println(" var = " + var + " not found");
return -1;
}
private int getPosVar(String[][]索引,String var)
{
int x=0;
布尔值=false;
if(index==null | | index.length==0 | | var==null)
返回-1;
而((x
因为您在执行检查后正在增加值。这会导致下一次检查太多。您只需添加这样的制动语句即可解决此问题
public static void main(String[] args) {
String var = "c";
String[][] index = {{"a","b","c"},{"a","b","c"}};
int x = 0;
boolean cond = false;
while( (index[0][x] != null) && (cond != true) )
{
if (index[0][x].contains(var) == true)
{
cond = true;
break;
}
x++;
}
System.out.println("x = " +x+ " val = " +index[0][x]);
此外,您还应始终尝试使用forloop而不是while循环。如果您检查数组的长度,则更难获得此类错误。因为您在执行检查后增加了值。这会导致下一次检查太多。您只需添加这样的制动语句即可解决此问题
public static void main(String[] args) {
String var = "c";
String[][] index = {{"a","b","c"},{"a","b","c"}};
int x = 0;
boolean cond = false;
while( (index[0][x] != null) && (cond != true) )
{
if (index[0][x].contains(var) == true)
{
cond = true;
break;
}
x++;
}
System.out.println("x = " +x+ " val = " +index[0][x]);
此外,您还应该始终尝试使用forloop而不是while循环。如果您检查数组的长度,则很难获得类似这样的错误。您可以使用旧程序 公共类TwoDStringArray{
static String[][] index = new String[1][3];
public static void main(String[] args) {
index[0][0] = "amal";
index[0][1] = "dev";
int x = 0;
boolean cond = false;
String var = "dev";
while((index[0][x] != null) && (cond != true))
{
if (index[0][x++].equals(var) == true)
{
cond = true;
x--;
}
}
System.out.println("x = " +x+ " val = " +index[0][x] + " "+ cond);
}
}
O/p-->
x=1 val=dev true
但是你有