从不同的方法调用数组,为什么java.lang.ArrayIndexOutOfBoundsException?

时间:2012-06-24 04:22:32

标签: java arrays methods

问题:我的阵列有什么问题,我该如何解决?

详细说明: 我在main方法中初始化了数组,并在一个方法中设置了值。我在第二种方法中调用了数组值,一切都很好。 当我尝试在第三种方法中调用数组时,我得到了越界错误,即使数组的大小完全相同。 我试图调用数组以便复制它,然后对第二个数组进行排序。

谢谢

private static  WeatherLocation[] WeatherSpots = new WeatherLocation[6];
private static Scanner Input = new Scanner(System.in);

public static void main(String[] args) 
{int Count;

for(Count = 0 ; Count < 6; Count++)
    WeatherSpots[Count] = new WeatherLocation();

WeatherSpots[0].LocationID = "Tciitcgaitc";
WeatherSpots[1].LocationID = "Redwood Haven";
WeatherSpots[2].LocationID = "Barrier Mountains";
WeatherSpots[3].LocationID = "Nina's Folly";
WeatherSpots[4].LocationID = "Scooly's Hill";
WeatherSpots[5].LocationID = "Twin Cones Park";
    SetUp();

    String Command = "";
    while(!Command.equals("Quit"))  {
    Menu();

    System.out.print("Enter Command: ");
    Command = Input.nextLine();

    if(Command.equals("Post"))
        PostTemperatureInfo();
    if(Command.equals("Daily"))
        WeeklyReport();
    else if (Command.equals("HighLow"))
        Sorting();
    }
}

public static void PostTemperatureInfo()
{
    Scanner LocalInput = new Scanner(System.in); 
    int K;
    int Temp;
    //...then get the values for each location...

    System.out.println( "Enter the Temperature for each weather station below:\n");
    System.out.println( "---------------------------------------------------------------");

    for(K = 0 ; K < 6 ; K++)  {
        System.out.println( "Weather Station: " + WeatherSpots[K].LocationID);  //Display the location of the fishing spot...

        System.out.print( "Enter Temperature:\t");   //Get the count...
        Temp = LocalInput.nextInt();
         System.out.println( "---------------------------------------------------------------");
         WeatherSpots[K].CatchCount = Temp;
        }
    System.out.println("");
     System.out.println("");
     System.out.println("");
}
public static void WeeklyReport()
{
    for(K = 0 ; K < 6 ; K++) 
        {System.out.println( "" + WeatherSpots[K].LocationID +"\t\t" + WeatherSpots[K].CatchCount + "\t\t" + String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9));
}
}

public static void Sorting()
{int K = 0; 

for(K = 0 ; K < 6 ; K++);
    {int [] copycat = new int[K];


    System.arraycopy(WeatherSpots[K].CatchCount, 0, copycat[K], 0, 6);
    System.out.println("" + copycat[K]);
    Arrays.sort(copycat, 0, K);
    System.out.println("Minimum = " + copycat[0]); 
System.out.println("Maximum = " + copycat[K -1]);  

        }
    }
}

2 个答案:

答案 0 :(得分:1)

问:为什么不使用“array.length”而不是硬编码的“6”?

问:如果可以避免,我真的不鼓励你使用缩进样式。

无论如何 - 这应该有用(我自己尝试过):

public static void Sorting() {
  for(int K = 0 ; K < WeatherSpots.length ; K++) {
     int [] copycat = new int[K];

     System.arraycopy(
       WeatherSpots[K].CatchCount, 0, copycat[K], 0, WeatherSpots.length);
     System.out.println("" + copycat[K]);
     Arrays.sort(copycat, 0, K);
     System.out.println("Minimum = " + copycat[0]); 
     System.out.println("Maximum = " + copycat[K -1]);  
  }
}

最主要的是摆脱无关的“;”在“for()”循环之后。

答案 1 :(得分:1)

问题是你分配的数组copycat只有K个整数,然后你试图将6个元素放入其中,即使K == 0.我不喜欢理解你的代码足以弄清楚正确的索引是什么,但这是你问题的根源。

实际上,我不相信你发布的代码会编译。此行来自Sorting()

System.arraycopy(WeatherSpots[K].CatchCount, 0, copycat[K], 0, 6);
似乎很可疑。 System.arraycopy的第一个和第三个参数应该是数组,但copycat[K]int。显然是WeatherSpots[K].CatchCount

编辑:

从您的评论和代码中可以看出,Sorting()例程只是打印WeatherSpots[K].CatchCount的最小值和最大值。这比你正在做的更容易。这是一种方式:

public static void Sorting() {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (WeatherLocation loc : WeatherSpots) {
        final int count = loc.CatchCount;
        if (count < min) {
            min = count;
        }
        if (count > max) {
            max = count;
        }
    }
    System.out.println("Minimum = " + min); 
    System.out.println("Maximum = " + max);
}