如何将数组移到左边?

时间:2017-04-07 19:15:34

标签: java arrays methods

它正确编译,但是当它运行时,会打印出数字数组,然后是一堆错误。我怎么能让leftShift转左?

g++ -c main.cpp
cc   main.o   -o main
main.o: In function `main':
main.cpp:(.text+0x23): undefined reference   std::allocator<char>::allocator()
...
main.cpp:(.text+0xb6): undefined reference to `BigNum::BigNum(double)'
main.cpp:(.text+0x2d9): undefined reference to `std::ios_base::Init::~Init()'
main.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'main' failed
make: *** [main] Error 1

2 个答案:

答案 0 :(得分:0)

您希望获得的输出是多少?

是“-10 23 -3 78”吗?

或循环移位左“-10 23 -3 78 95”?

我会选择循环并实现如下:

public static void leftShift(int[] list) {
  int first = list[0];

  for (int i = 0; i < list.length - 1; i++) {
    list[i] = list[i+1];
  }

  list[list.length - 1] = first;
}

答案 1 :(得分:0)

public static int[] leftShift(int[] a) {
    int[] r = new int[a.length];
    for (int i = 0; i < a.length - 1; i++)
        r[i] = a[i+1];
    r[a.length - 1] = a[0];
    return r;
}

如果你想将所有东西都移到左边并将第一个值包回到最后,那么这应该有效....