簡體   English   中英

向左移動數組中的數據

[英]Moving data in an array to the left

我對Java真的很陌生,代碼有問題。 未檢測到錯誤,但輸出為奇數。 目標是將數組中的數據向左移動。 例如:x = {1,2,3},新數組應為{2,3,1}。

現在,下面的代碼僅給我{0,0,0}。 如果您指出錯誤並告訴我該怎么辦會很好。 非常感謝!

public class Project1 {

public static int[] shiftone(int[]n,boolean left) {
    n = new int[n.length];


    int save,save2;
    if(left = true){
        save = n[0];
        save2 = n[(n.length-1)];
        for (int i = 1; i < n.length-1; i++) {
            n[i-1]=n[i];
        }
        n[n.length-1] = save;
        n[n.length-2] = save2;
    }
    else{
        save = n[n.length-1];
        for (int i=0;i<(n.length-1);i++)
            n[(n.length)-i] = n[(n.length-1)-1];
       n[0] = save; 
    }
    return n;
}

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int[] x;
    int k;

    boolean left;
    System.out.print("Masukkan jumlah data yang ingin diinput: ");
    k = input.nextInt();
    System.out.println();
    x = new int[k];

    for (int i = 0; i < k; i++) {
    System.out.print("Input data ke-"+i+": ");
    x[i] = input.nextInt(); 
    }
    System.out.print("Array: "+Arrays.toString(x));
    System.out.println();

    System.out.print("Move to left? (true/false): ");
    left = input.nextBoolean();
    System.out.println();

    int[] y;
    y = new int[k];
    y = shiftone(x,left);
    System.out.print("New array: "+Arrays.toString(y));
    }
}

作為您目標的簡單解決方案,您可以使用此

public static int[] shiftone(int[] n, boolean left) {
    // you don't need to shift anything if length = 1
    if (n.length < 2) {
        return n;
    }
    if (left) {
        // save first element
        int save = n[0];
        for (int i = 1; i < n.length; i++) {
            // shift from 1 to n
            n[i-1] = n[i];
        }
        // insert saved element to array
        n[n.length - 1] = save;
    } else {
        // the same
        int save = n[n.length - 1];
        for (int i = 1; i < n.length; i++)
            n[n.length - i] = n[(n.length - 1) - i];
        n[0] = save;
    }
    return n;
}

有一種非常快速的方法將數組元素從一個位置復制到另一個位置。 我不知道這是否對您有幫助,因為在我看來您的問題是家庭作業。 不過,我會在代碼中加上適當的注釋...

public class Answer {

    public static void main(String[] args) {

        //test case
        int[] input = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(input));

        //save the first element in the temporary variable
        int temp = input[0];

        //the fastest way to copy the array elements
        //1st parameter is the source array
        //2nd parameter is the source position (read: from which element to copy)
        //3rd parameter is the destination (in this case the same array)
        //4th parameter is the destination position (read: where to store the 1st element)
        //5th parameter is the length of elements to copy (read: how many)
        System.arraycopy(input, 1, input, 0, input.length - 1);

        //finally store the saved element to the end
        input[input.length - 1] = temp;

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

}

如果我們不想自己編寫移動代碼,則可以使用Collections.rotate方法。 它獲取一個List並將元素旋轉給定距離。 要使用它,我們需要將int數組轉換為List<Integer> 旋轉后的列表將轉換回int數組。

protected static int[] move(int[] input, int distance) {
    List<Integer> inputList = Arrays.stream(input).boxed().collect(Collectors.toCollection(ArrayList::new));
    Collections.rotate(inputList, distance);
    return inputList.stream().mapToInt(Integer::intValue).toArray();
}

用法:

public static void main(String[] args) throws Exception {
    int[] input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    int moveLeftOnce = -1;
    int[] moved = move(input, moveLeftOnce); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
}

請注意:
由於Collections.rotate將移動給定列表中的元素,因此該列表必須是可修改的。 ArrayList就是這種情況。 因此代碼使用Collectors.toCollection(ArrayList::new)因為存在( JavaDoc

不保證返回List的類型,可變性...

通過Collectors.toList

暫無
暫無

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

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