簡體   English   中英

我在這里用選擇排序做錯了什么。 不斷給我有關類型的問題

[英]What am I doing wrong here with the selection sort. Keeps giving me issues about the type

import java.util.Scanner;
import java.io.*;

public class SortingforFun {
    static final String IN_FILE = "sorting.txt";
    static final int ARRAY_SIZE = 20000;

    public static void  main(String[] arg) {

        long[] arr1= new long [ARRAY_SIZE];


        long[] arr2 = (long[])arr1.clone();



        long startTime = System.startHere();



        int i;

        System.out.println("\n\n RoseIndia\n\n");
        System.out.println(" Selection Sort\n\n");
        System.out.println("Values Before the sort:\n");
        for(i = 0; i < arr1.length; i++)
        System.out.print( arr1[i]+"  ");
        System.out.println();
        selection_srt(ARRAY_SIZE, arr1.length);
        System.out.print("Values after the sort:\n");
        for(i = 0; i < arr1.length; i++)
        System.out.print(arr1[i]+"  ");
        System.out.println();
        System.out.println("PAUSE");
    }

    public static void selection_srt(long ARRAY_SIZE , long n){
        for(long x=0; x<n; x++){
            long index_of_min = x;
            for(long y=x; y<n; y++){
                if(ARRAY_SIZE[(index_of_min]< ARRAY_SIZE[y]){
                    index_of_min = y;
                }
            }
            long temp = ARRAY_SIZE[x];
            ARRAY_SIZE[x] = ARRAY_SIZE[index_of_min];
            ARRAY_SIZE[index_of_min] = temp;
        }
    }
}

selection_sort方法,你得到一個參數ARRAY_SIZE型的long ,並試圖使用它作為一個數組。 我認為這就是引起您引用的類型警告的原因(盡管如果顯示了所得到的特定錯誤消息會更清楚)。

我猜想您希望該方法具有如下簽名:

public static void selection_srt( long[] array, long n )

並在調用時傳遞arr1arr2

您也沒有將任何實際數據放入這些數組,盡管這不會導致錯誤。 所有元素將自動初始化為0,因此對它們進行排序不會很有趣。

使用帶有方括號[]的長ARRAY_SIZE好像它是一個數組已經是一個錯誤。 我不認為您的代碼可以編譯

另外,您還有一個名稱重影問題-參數ARRAY_SIZE(長類型)正在重影具有相同名稱的靜態成員,盡管類型為int。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM