当我尝试创建一个数组时,它会给我一个错误"这个数组只能从"中读取,为什么我不能在其中写入?

时间:2015-12-19 13:36:11

标签: java arrays arraylist

任务是:编写一个java程序,该程序从用户获取整数输入流并找到它们之间的最小距离 流中的两个相邻数字。 它应该输出两个相邻数字的索引。

这是代码,我在arraylist中存储了一些数字,然后我拿了arraylist的最后一个索引并尝试创建一个具有相同数量元素的数组

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Pr6 
{
public Pr6 ()
{
    Scanner sc = new Scanner (System.in);
    String x = "";
    int Distance =0;
    int Index1 = 0;
    int Index2 = 0;
    List<Integer> AllNum = new ArrayList<>();
    do 
    {
        System.out.println ("Please enter a number");
        int NewNumber = sc.nextInt();
        AllNum.add (NewNumber);
        System.out.println ("Do you want to enter a new number? Enter Y for yes and N for no");
        x = sc.nextLine();
    } while ("Y".equals(x));

    int Index = AllNum.size();
    int [] AllNumArray = new int [Index];

    for (int i = 0; i < Index; i++)
    {
        AllNumArray[i] = AllNum.get(i);
    }

    for (int i = 0; i < Index; i++)
    {
        int NewDistance = AllNumArray[i] - AllNumArray [i+1];
        if (NewDistance > Distance)
        {
            Distance = NewDistance;
            Index1 = i;
            Index2 = i+1;
        }
    }

    System.out.println ("The smallest distance between two neighboring numbers is: " + Distance);
    System.out.println("The first Nnumber is " + AllNumArray[Index1] + ", and its index is " + Index1);
    System.out.println("The second Nnumber is " + AllNumArray[Index2] + ", and its index is " + Index2);
} 

}

1 个答案:

答案 0 :(得分:0)

你可以得到如下的最后一个对象:

ArrayList<Integer> AllNum = new ArrayList<>();
        do
        {
            System.out.println ("Please enter a num");
            AllNum.add (sc.nextInt());
            System.out.println ("Do you want to enter a new sentence? Enter Y for yes and N for no");
            x = sc.nextLine();
        } while ("Y".equals(x));
        int Index = AllNum.get(AllNum.size() - 1); // change is here
        int [] AllNumArray = new int [Index];

x list x的{​​{1}}索引为string Y,因此它不在列表中,因此returns -1。查看JavaDoc

public int lastIndexOf(@ org.jetbrains.annotations.Nullable java.lang.Object o) 返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回-1。更正式地,返回最高索引i,使得(o == null?get(i)== null:o.equals(get(i))),或-1,如果没有这样的索引。

编辑:重写代码:

Scanner sc = new Scanner(System.in);
String x; // no need to init here String x = "";
int Distance = 0;
int Index1 = 0;
int Index2 = 0;
List<Integer> AllNum = new ArrayList<>();
do {
    System.out.println("Please enter a number");
    int NewNumber = sc.nextInt();
    AllNum.add(NewNumber);
    System.out.println("Do you want to enter a new number? Enter Y for yes and N for no");
    x = sc.next(); // here changed sc.nextLine() -> sc.next()
} while (x.equalsIgnoreCase("y")); // here changed equals() -> equalsIgnoreCase()

int Index = AllNum.size();
int[] AllNumArray = new int[Index];
for (int i = 0; i < Index; i++) {
    AllNumArray[i] = AllNum.get(i);
}
for (int i = 0; i < Index; i++) {
    // here changed, and the main problem was here,
    int nextIndex = i < AllNumArray.length - 1 ? i + 1 : 0;
    int NewDistance = AllNumArray[i] - AllNumArray[nextIndex];
    if (NewDistance > Distance) {
        Distance = NewDistance;
        Index1 = i;
        Index2 = i + 1;
    }
}
System.out.println("The smallest distance between two neighboring numbers is: " + Distance);
System.out.println("The first Nnumber is " + AllNumArray[Index1] + ", and its index is " + Index1);
System.out.println("The second Nnumber is " + AllNumArray[Index2] + ", and its index is " + Index2);