简体   繁体   English

错误:越界 Java 冒泡排序

[英]Error: Out of bounds Java Bubble sort

public void sort()  // pulls the numbers off the stack then orders them, putting back onto stack in order. 
                    // Does not care where the it left off. Note: making a temp varible for the new head will head. 
                    // once the old head hits a -1; or a value less than it self it will start from the "sort head"
                    // ????????????????????????????????????????????????????????????????????????????????????????????????
{
     int[] numbers = new int[node - 1];    //everything else should be working fine. 

     for(int i = size ; i > 1; i--)        //for loop for the numberof/size of the elements needed to be sorted.3
     {
         int temphead = head;

         numbers[i] = (stack.getpeople(temphead));  // pulls a value for a stack

         temphead = stack.getBLink(temphead);       // pulls that values back link and adds it to the new lead.
     }

    int tempVar;

    for (int i = 0; i < numbers.length; i++) //used for bubble sort.
    {
             for(int j = 0; j < numbers.length - 1; j++) // used for bubble sort.
             {
                     if(numbers[i] > numbers[j + 1]) //ERROR HERE Out of bounds
                     {
                     tempVar = numbers [j + 1];      //ERROR HERE Out of bounds
                     numbers [j + 1] = numbers [i];  //ERROR HERE Out of bounds
                     numbers [i] = tempVar;          //ERROR HERE Out of bounds
                     }
             }
    }
    int temphead = head;

    for(int i = size ; i > 1; i--)
    {
        stack.getpeople(temphead = numbers[i]); //adds the sorted list back to the stack 
        temphead = stack.getBLink(temphead); //resets the temp head. 

    }
}

//The above is error is marked out of bounds. //上面是错误被标记为越界。 it is probably something really simple that i missing, I do not believe it is the bubble sort function it self due to the fact that the logic is right?这可能是我遗漏的非常简单的事情,我不相信这是冒泡排序功能本身,因为逻辑是正确的? however it may be an outbound error do to a condensation not programmed in my stack, if that is the case i will have to update this post with the stack and some of the other methods.但是,对于未在我的堆栈中编程的冷凝,这可能是一个出站错误,如果是这种情况,我将不得不使用堆栈和其他一些方法更新这篇文章。 -----Thanks for the help, - - -谢谢您的帮助,

The error is one line before you think -错误是你想之前的一行 -

for(int j = 0; j < numbers.length - 1; j++) { // <--- The error was here.
  // used for bubble sort.
  if(numbers[i] > numbers[j + 1]) // When j = (number.length - 1) [0 based indexing], 
                                  // then j + 1 = (number.length) and that is 
                                  // out of bounds.

当您调用 numbers[j+1] 时,您将超出数组范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM