Java 找不到"&引用;数组列表中的符号
在数组列表中找不到“.”符号Java 找不到"&引用;数组列表中的符号,java,arraylist,Java,Arraylist,在数组列表中找不到“.”符号 public class ArrayTools { //instance variables and constructors could be present, but are not necessary //sumSection will return the sum of the numbers //from start to stop, not including stop public static int sumSection(int[] n
public class ArrayTools
{
//instance variables and constructors could be present, but are not necessary
//sumSection will return the sum of the numbers
//from start to stop, not including stop
public static int sumSection(int[] numArray, int start, int stop)
{
int sum=0;
for ( int i = start; i < stop; i++)
{
sum += numArray.get(i);
}
return sum;
}
公共类数组工具
{
//实例变量和构造函数可能存在,但不是必需的
//sumSection将返回这些数字的总和
//从开始到停止,不包括停止
公共静态int sumssection(int[]numArray,int start,int stop)
{
整数和=0;
for(int i=开始;i<停止;i++)
{
sum+=numaray.get(i);
}
回报金额;
}
get()
是列表的一种方法
这是您首先应该尝试更改的内容,以修复“
错误。然后您会遇到另一个问题。将数组转换为列表是冗余的,因此您可以将其移出循环
如果在循环中使用之前预先初始化列表,则必须在方法签名中使用Integer[]
而不是int[]
,因为Arrays.asList()
将数组转换为列表时不正确
public static int sumSection(Integer[] numArray, int start, int stop)
{
int sum=0;
List<Integer> numList = Arrays.asList(numArray);
for ( int i = start; i < stop; i++)
{
sum += numList.get(i);
}
return sum;
}
public static int sumSection(整数[]numArray,int start,int stop)
{
整数和=0;
List numList=Arrays.asList(numArray);
for(int i=开始;i<停止;i++)
{
sum+=numList.get(i);
}
回报金额;
}
您已经尝试从数组中使用get()
方法,但数组是不实现List
接口的对象
您误解了numArray.get(i)
是错误的,numArray
不是一个List
。如果您想使用List及其get()
方法,您应该在使用此对象的get()
之前将其转换为List
。get是List方法,而不是数组方法
使用:numArray[i]
尝试使用numArray[i]
而不是numArray。获取(i)
像这样使用是为了列表
:
sum += numArray.get(i);
但在您的情况下,请将其用于数组
:
sum += numArray[i];
numArray
是一个数组,请尝试numArray[i]
而不是numArray.get(i)
。包含代码很好。包含一个问题更好。为什么要创建一个冗余的ArrayList?为什么不只创建numArray[i]?您的代码在运行时要么不会编译,要么会导致异常。Arrays.asList(numArray)的结果
不是List
@NirLevy OP使用数组列表的要点。@RomanC不,他不是。OP在标题中写了“数组列表”,但我不认为他知道区别。@Tom在OP写numArray时。get(I)
他误解了numArray
不是列表。