需要帮助排序数组列表

时间:2022-11-26 02:07:08

I am very new to java and I need to create an array that stores numbers and then outputs the numbers in sorted list the mean mode and median.

我是java的新手,我需要创建一个存储数字的数组,然后输出排序列表中的数字,即平均模式和中位数。

can anyone tell me if I am on the right track so far with the code below

任何人都可以告诉我,到目前为止我是否在正确的轨道上使用下面的代码

int number=0;

ArrayList<Integer> listOfNumbers = new ArrayList<>();
Scanner scannerStream = new Scanner (System.in);

System.out.println("Enter list of numbers to Sort: (OR * TO  END LIST)");
while(!(listOfNumbers = br.readLine()).equals("*"))
     {

listOfNumbers = scannerStream.nextInt();
NumberList = listOfNumbers();

public Vector listOfNumbersSort (int number) {

public Vector listOfNumbersSort(int number){

    for (int i=0; i<NumberList; i++) {

         int Sort = listOfNumbers();;


return Sort;

// returns the mean
public Vector<Integer>  listOfNumbersMean (int number){

    return mean;
} // end 

// returns the mode 
public Vector<Integer>  listOfNumbersMode (int number){

    return mode;
} // end 

// returns the median
public Vector<Integer>  listOfNumberMedian (int number){

    return median;
} // end 

Thanks in advance for any help for advice provided

提前感谢您提供任何建议的帮助

2 个解决方案

#1


0  

can anyone tell me if I am on the right track so far with the code below

Your method definitions does not look good. You are passing a number and expecting the sortedlistofnumbers. Offcourse, if you already have a list, and you sorted it, and then if you get a new number and need to re-sort, then you can have the method signature as you have written, but not at the primary level.

您的方法定义看起来不太好。您正在传递一个数字并期望sortedlistofnumbers。当然,如果你已经有一个列表,并对它进行了排序,然后如果你得到一个新的数字并需要重新排序,那么你可以按照你所写的方法签名,但不能在主要级别。

The way you are accepting the list of number is not right. You need a loop. And when you use Arrays, since Arrays are fixed length, you should specify the size too.

你接受号码列表的方式是不对的。你需要一个循环。当你使用Arrays时,由于Arrays是固定长度的,你也应该指定它的大小。

see the below code for reference.

请参阅以下代码以供参考。

If you want to implement sorting in your way, there are various alogorithms like bubble sort.

如果你想以你的方式实现排序,有各种算法,如冒泡排序。

Sorting Arrays

排序数组

public class Sorter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new InputStreamReader(System.in));
        System.out.println("How many numbers to sort ?");
        int count = scanner.nextInt();
        int numbers[] = new int[count];
        System.out.println("Enter list of numbers to Sort: ");
        for (int i = 0; i < count; i++) {
            System.out.println("Enter number");
            numbers[i] = scanner.nextInt();
        }

        System.out.println("List before sorting..");
        for (int i = 0; i < count; i++) {
            System.out.println(numbers[i]);
        }
        Arrays.sort(numbers);

        System.out.println("Sorted list");
        for (int i = 0; i < count; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Sorting ArrayList

对ArrayList进行排序

public class SorterList {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new InputStreamReader(System.in));
        System.out.println("How many numbers to sort ?");
        int count = scanner.nextInt();
        List<Integer> numbers = new ArrayList<Integer>();
        System.out.println("Enter list of numbers to Sort: ");
        for (int i = 0; i < count; i++) {
            System.out.println("Enter number");
            numbers.add(scanner.nextInt());
        }
        System.out.println("List before sorting..");
        System.out.println(numbers);
        Collections.sort(numbers);
        System.out.println("Sorted list");
        System.out.println(numbers);
    }
}

Edit:

编辑:

If you want to do custom sorting - say bubble sort algorithm,

如果你想做自定义排序 - 比如冒泡排序算法,

        int temp;
        for (int i = 0; i < (count - 1); i++) {
            for (int j = 0; j < count - i - 1; j++) {
                if (numbers[j] > numbers[j + 1]) // '> for ascending order'
                {
                    temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }

#2


1  

Use Collections.sort() to sort an ArrayList.

使用Collections.sort()对ArrayList进行排序。

Use Arrays.sort() to sort an array.

使用Arrays.sort()对数组进行排序。

#1


0  

can anyone tell me if I am on the right track so far with the code below

Your method definitions does not look good. You are passing a number and expecting the sortedlistofnumbers. Offcourse, if you already have a list, and you sorted it, and then if you get a new number and need to re-sort, then you can have the method signature as you have written, but not at the primary level.

您的方法定义看起来不太好。您正在传递一个数字并期望sortedlistofnumbers。当然,如果你已经有一个列表,并对它进行了排序,然后如果你得到一个新的数字并需要重新排序,那么你可以按照你所写的方法签名,但不能在主要级别。

The way you are accepting the list of number is not right. You need a loop. And when you use Arrays, since Arrays are fixed length, you should specify the size too.

你接受号码列表的方式是不对的。你需要一个循环。当你使用Arrays时,由于Arrays是固定长度的,你也应该指定它的大小。

see the below code for reference.

请参阅以下代码以供参考。

If you want to implement sorting in your way, there are various alogorithms like bubble sort.

如果你想以你的方式实现排序,有各种算法,如冒泡排序。

Sorting Arrays

排序数组

public class Sorter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new InputStreamReader(System.in));
        System.out.println("How many numbers to sort ?");
        int count = scanner.nextInt();
        int numbers[] = new int[count];
        System.out.println("Enter list of numbers to Sort: ");
        for (int i = 0; i < count; i++) {
            System.out.println("Enter number");
            numbers[i] = scanner.nextInt();
        }

        System.out.println("List before sorting..");
        for (int i = 0; i < count; i++) {
            System.out.println(numbers[i]);
        }
        Arrays.sort(numbers);

        System.out.println("Sorted list");
        for (int i = 0; i < count; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Sorting ArrayList

对ArrayList进行排序

public class SorterList {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new InputStreamReader(System.in));
        System.out.println("How many numbers to sort ?");
        int count = scanner.nextInt();
        List<Integer> numbers = new ArrayList<Integer>();
        System.out.println("Enter list of numbers to Sort: ");
        for (int i = 0; i < count; i++) {
            System.out.println("Enter number");
            numbers.add(scanner.nextInt());
        }
        System.out.println("List before sorting..");
        System.out.println(numbers);
        Collections.sort(numbers);
        System.out.println("Sorted list");
        System.out.println(numbers);
    }
}

Edit:

编辑:

If you want to do custom sorting - say bubble sort algorithm,

如果你想做自定义排序 - 比如冒泡排序算法,

        int temp;
        for (int i = 0; i < (count - 1); i++) {
            for (int j = 0; j < count - i - 1; j++) {
                if (numbers[j] > numbers[j + 1]) // '> for ascending order'
                {
                    temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }

#2


1  

Use Collections.sort() to sort an ArrayList.

使用Collections.sort()对ArrayList进行排序。

Use Arrays.sort() to sort an array.

使用Arrays.sort()对数组进行排序。