Java:数组大小的插入排序问题

Java:数组大小的插入排序问题,java,arrays,sorting,methods,parameters,Java,Arrays,Sorting,Methods,Parameters,我试图编写一个排序程序,询问用户使用哪种排序方法(插入、冒泡、选择),然后让用户输入整数进行排序 我认为除了数组之外,我所有的东西都是正确的:我希望数组的大小与用户输入的整数的数量一样大,但我似乎做得不正确 在排序类中,insertionSort方法所在的位置,我应该将输入参数命名为那样的名称(通过整个算法),还是应该使用“arr”这样的通用名称 在哪里可以改进和更正代码 谢谢你的帮助 驾驶员分类: import java.util.Scanner; public class DriverSo

我试图编写一个排序程序,询问用户使用哪种排序方法(插入、冒泡、选择),然后让用户输入整数进行排序

我认为除了数组之外,我所有的东西都是正确的:我希望数组的大小与用户输入的整数的数量一样大,但我似乎做得不正确

在排序类中,insertionSort方法所在的位置,我应该将输入参数命名为那样的名称(通过整个算法),还是应该使用“arr”这样的通用名称

在哪里可以改进和更正代码

谢谢你的帮助

驾驶员分类:

import java.util.Scanner;

public class DriverSort 
{
    public static void main(String[] args) 
    {
        Scanner scan =new Scanner(System.in);
        Sorter sorter = new Sorter();

        int choice;// variable which says which sorting algorithm to use

        System.out.println("1-Insertion sort\n"
                    +"2-Selection sort\n"
                    + "3-Bubble sort\n"
                    + "0-quit\n");
        int size = scan.nextInt();
        int input[] = new int[size];

        System.out.println("Please enter the number for a sorting method or enter 0 to quit: ");
        size = scan.nextInt();


        System.out.println("\nBefore Sorting: ");
        sorter.printArray(input);

        // sort the array
        Sorter.insertionSort(input);
        System.out.println("\nAfter Sorting: ");
        sorter.printArray(input);

        switch (choice) 
        {
            case 0:
                System.out.println("Goodbye!");
                System.exit(0);
                break;

            case 1: 
                Sorter.insertionSort(input);
                sorter.printArray(input);
                break;
        }
   }
}
分拣机等级:

public class Sorter
{
    public static int[] insertionSort(int[] input) 
    {
        for (int i = 1; i < input.length; i++) 
        {
            int valueToSort = input[i];
            int j = i;

            while (j > 0 && input[j - 1] > valueToSort) 
            {
                input[j] = input[j - 1];
                j--;
            }//end while loop.

            // insert the element
            input[j] = valueToSort;
        }//end for loop    

        return input;
    }//end insertionSort          

    public void printArray(int[] input) 
    { 
        System.out.println(input.toString());
    }
}   
公共类分拣机
{
公共静态int[]插入排序(int[]输入)
{
for(int i=1;i0&&input[j-1]>valueToSort)
{
输入[j]=输入[j-1];
j--;
}//边结束边循环。
//插入元素
输入[j]=值排序;
}//循环结束
返回输入;
}//末端插入端口
公共void打印数组(int[]输入)
{ 
System.out.println(input.toString());
}
}   

我看到的第一件事是

int size = input.length;
int input[] = new int[size];
这是非常非法的,我停止了阅读。硬编码大小,或提示用户输入

int size = 10; // <-- 10
int input[] = new int[size]; // <-- this is fine.

我建议完全删除Sorter类,并将Sorter类的功能作为方法添加到DriverSorter中。我这样说是因为您实现Sorter的方式不会创建有用的实例

import java.util.Scanner;

public class DriverSort {

    public static void main(String[] args) {

        Scanner scan =new Scanner(System.in);

        // this makes more sense to put this at the start of the program
        int choice;  // variable which says which sorting algorithm to use
        System.out.println("1-Insertion sort\n"
                +"2-Selection sort\n"
                + "3-Bubble sort\n"
                + "0-quit\n");
        choice = scan.nextInt();

        if (choice != 0) { // a simple if else statement will do just fine
            // must prompt user for the "input first"
            System.out.println("Enter the length vector to be modified: ");
            int size = scan.nextInt();
            // now actually get the vector
            int input[] = new int[size];
            for (int i = 0; i < size; i++) {
                System.out.println("Enter next array element: ");
                input[i] = scan.nextInt();
            }

            System.out.println("\nBefore Sorting: ");
            System.out.println(input); // use the builtin functionality
            // sort the array
            int[] output = insertionSort(input);
            System.out.println("\nAfter Sorting: ");
            System.out.println(output);
        } else { 
            System.out.println("Goodbye!");
            System.exit(0);
        }  
    }

    // returns a sorted list (add more detail here)
    // add a new input that tells what sort of sorting to do
    public static int[] insertionSort(int[] input) {

        for (int i = 1; i < input.length; i++) {
            int valueToSort = input[i];
            int j = i;
            while (j > 0 && input[j - 1] > valueToSort) {
                input[j] = input[j - 1];
                j--;
            }//end while loop.
            // insert the element
            input[j] = valueToSort;
        }//end for loop    
        return input;
    } 
}
import java.util.Scanner;
公共类DriverSort{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
//把它放在程序的开头更有意义
int choice;//表示要使用哪个排序算法的变量
System.out.println(“1-插入排序\n”
+“2-选择排序\n”
+“3气泡排序\n”
+“0-退出\n”);
choice=scan.nextInt();
if(choice!=0){//一个简单的if-else语句就可以了
//必须提示用户输入“先输入”
System.out.println(“输入要修改的长度向量:”);
int size=scan.nextInt();
//现在得到向量
int input[]=新的int[size];
对于(int i=0;i0&&input[j-1]>valueToSort){
输入[j]=输入[j-1];
j--;
}//边结束边循环。
//插入元素
输入[j]=值排序;
}//循环结束
返回输入;
} 
}
保持分拣机等级:

public class Sorter {
    private int vector; \\ private just means only things inside this class can affect this variable
    // now initializer 
    public Sorter(int[] input) {
        this.vector = input; \\ set our field to be equal to the vector you input when making an instance
    // so the call to make a Sorter object will now be "Sorter sorter = new Sorter(input);"
    }

    // make this act on vector instead
    public static int[] insertionSort() // no input because it can "see" our vector field
    {
        int[] copy = this.vector; // make a copy so you don't mess vector up before your finished
        for (int i = 1; i < copy.length; i++) 
        {
            int valueToSort = copy[i];
             int j = i;

            while (j > 0 && copy[j - 1] > valueToSort) 
            {
                copy[j] = copy[j - 1];
                j--;
            }//end while loop.

            // insert the element
            copy[j] = valueToSort;
        }//end for loop    

        this.vector = copy; // now replace old field with our sorted copy!
    }//end insertionSort          

    // this is an excellent way to be able to see "vector" without allowing other
    // mischievous programs to accidentally change "vector." This is very similar reasoning
    // to why you very frequently have fields be private. Read up on encapsulation - it's 
    // super useful.
    public void printArray(int[] input) 
    { 
    System.out.println(this.vector.toString());
    }
公共类分拣机{
private int vector;\\private只是表示只有该类中的内容才能影响该变量
//现在初始化器
公共分拣机(int[]输入){
this.vector=input;\\将我们的字段设置为与创建实例时输入的向量相等
//因此,创建分拣机对象的调用现在将是“分拣机分拣机=新分拣机(输入);”
}
//改为对向量执行此操作
public static int[]insertionSort()//没有输入,因为它可以“查看”向量场
{
int[]copy=this.vector;//复制一份,这样在完成之前就不会弄乱向量
for(int i=1;i0&©[j-1]>valueToSort)
{
复制[j]=复制[j-1];
j--;
}//边结束边循环。
//插入元素
复制[j]=值排序;
}//循环结束
this.vector=copy;//现在用已排序的副本替换旧字段!
}//末端插入端口
//这是一种很好的方式,可以在不允许其他操作的情况下查看“向量”
//恶作剧程序无意中改变了“向量”。这是非常相似的推理
//要了解为什么您经常将字段设置为私有字段,请阅读封装—这是一个很好的例子
//非常有用。
公共void打印数组(int[]输入)
{ 
System.out.println(this.vector.toString());
}

}

谢谢Elliot,是的,这正是我遇到的问题。第一部分是指大小为10的固定数组,第二部分是指使用用户输入的小于10的任何数字的最大大小为10的数组?实际上,第二种方法是我最初使用的方法,但我一直得到一个越界错误。@user3862586-Ummm。否。第一个是定义长度为
size
的数组,第二个是定义数组长度的
size
。它们不是直接等价的运算。我想要的是一个大小为用户输入多少个整数的数组。我看到了,我是个白痴。谢谢不过我还有一个问题。它现在可以工作,但这是排序前的输出:[I@7ea987ac排序后:[I@7ea987ac [I@7ea987acThe事情是c项目
import java.util.Scanner;

public class DriverSort {

    public static void main(String[] args) {

        Scanner scan =new Scanner(System.in);

        // this makes more sense to put this at the start of the program
        int choice;  // variable which says which sorting algorithm to use
        System.out.println("1-Insertion sort\n"
                +"2-Selection sort\n"
                + "3-Bubble sort\n"
                + "0-quit\n");
        choice = scan.nextInt();

        if (choice != 0) { // a simple if else statement will do just fine
            // must prompt user for the "input first"
            System.out.println("Enter the length vector to be modified: ");
            int size = scan.nextInt();
            // now actually get the vector
            int input[] = new int[size];
            for (int i = 0; i < size; i++) {
                System.out.println("Enter next array element: ");
                input[i] = scan.nextInt();
            }

            System.out.println("\nBefore Sorting: ");
            System.out.println(input); // use the builtin functionality
            // sort the array
            int[] output = insertionSort(input);
            System.out.println("\nAfter Sorting: ");
            System.out.println(output);
        } else { 
            System.out.println("Goodbye!");
            System.exit(0);
        }  
    }

    // returns a sorted list (add more detail here)
    // add a new input that tells what sort of sorting to do
    public static int[] insertionSort(int[] input) {

        for (int i = 1; i < input.length; i++) {
            int valueToSort = input[i];
            int j = i;
            while (j > 0 && input[j - 1] > valueToSort) {
                input[j] = input[j - 1];
                j--;
            }//end while loop.
            // insert the element
            input[j] = valueToSort;
        }//end for loop    
        return input;
    } 
}
public class Sorter {
    private int vector; \\ private just means only things inside this class can affect this variable
    // now initializer 
    public Sorter(int[] input) {
        this.vector = input; \\ set our field to be equal to the vector you input when making an instance
    // so the call to make a Sorter object will now be "Sorter sorter = new Sorter(input);"
    }

    // make this act on vector instead
    public static int[] insertionSort() // no input because it can "see" our vector field
    {
        int[] copy = this.vector; // make a copy so you don't mess vector up before your finished
        for (int i = 1; i < copy.length; i++) 
        {
            int valueToSort = copy[i];
             int j = i;

            while (j > 0 && copy[j - 1] > valueToSort) 
            {
                copy[j] = copy[j - 1];
                j--;
            }//end while loop.

            // insert the element
            copy[j] = valueToSort;
        }//end for loop    

        this.vector = copy; // now replace old field with our sorted copy!
    }//end insertionSort          

    // this is an excellent way to be able to see "vector" without allowing other
    // mischievous programs to accidentally change "vector." This is very similar reasoning
    // to why you very frequently have fields be private. Read up on encapsulation - it's 
    // super useful.
    public void printArray(int[] input) 
    { 
    System.out.println(this.vector.toString());
    }