簡體   English   中英

將數組元素移動到索引 0 並從左到右移動元素

[英]Move array element to index 0 and move elements left to right

所以主要思想是,輪盤賭機上的最后一個數字將在 Index[0] 上,索引“whatNumberCame”處的元素必須成為之前的數字

public class Main {
    public static void main(String[] args) {
        int[] rouletteNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter number : ");
        int whatNumberCame = scanner.nextInt();
        int collisionIndex;

        for(int i = 0; i < rouletteNumbers.length ; i++){
            if(rouletteNumbers[i] == whatNumberCame){
                System.out.println("COLLISION AT " + rouletteNumbers[i]);
                collisionIndex = rouletteNumbers[i];
                System.out.println(collisionIndex);
                for (int j = collisionIndex + 1; j <= 0 ; j--){
                    rouletteNumbers[j] = rouletteNumbers[j - 1];
                    System.out.print(rouletteNumbers);
                }
            }
        }

    }
}

例如如果我輸入 10 數組應該變成: {10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19 , 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}

您可以像這樣調整代碼以使其正常工作:

public static void main (String [] args) {
    int[] rouletteNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter number : ");
    int whatNumberCame = scanner.nextInt();

    for(int i = 0; i < rouletteNumbers.length ; i++){
        if(rouletteNumbers[i] == whatNumberCame){
            for (int j = i; j > 0; j--){
                rouletteNumbers[j] = rouletteNumbers[j - 1];
            }
            rouletteNumbers[0] = whatNumberCame;
            break;
        }
    }

    System.out.println(Arrays.toString(rouletteNumbers));
}

在您的循環中,您使用了j <= 0 ,它始終為false ,因此您的內部循環根本不執行。

當您找到數字的位置時,您可以使用break循環。

這是一個執行類似操作的方法,除了它允許您在您喜歡的任何數組索引處插入一個值。 將每個值插入數組后,數組的長度將按照您的帖子示例中的指示增長。

用法:

要在索引 0(數組的開頭)處向數組添加一個值:

rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, 0);

在數組的最后添加一個值:

rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, rouletteNumbers.length);

要將值插入數組中的任何索引(例如索引 4):

rouletteNumbers = insertIntoArray(rouletteNumbers, whatNumberCame, 4);

方法代碼:

public static int[] insertIntoArray(int[] array, int valueToInsert, int insertIntoIndex) {
    /* If the inertIntoIndex value is greater than the supplied Array Length 
       then the inertIntoIndex value is changed to the supplied Array Length.
       This ensures that the valueToInsert will be place at the end of the
       Array.            */
    if (insertIntoIndex > array.length) {
        insertIntoIndex = array.length;
    }
    int[] tmp = new int[array.length + 1];
    int idxCounter = 0;
    int i = 0;
    for (; i < array.length; i++) {
        if (i == insertIntoIndex) {
            tmp[idxCounter] = valueToInsert;
            idxCounter++;
        }
        tmp[idxCounter] = array[i];
        idxCounter++;
    }
    /* when at this point in code, the condition below 
       indicates that the desired insert index location 
       would be at the very end of the array.        */
    if (i == idxCounter) {
        tmp[idxCounter] = valueToInsert;
    }
    return tmp;
}

暫無
暫無

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

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