Java中根据索引号将元素移动到数组前面

3
假设我有一个数组:
int[] taco = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

我该如何根据索引将元素移到前面?例如:
将taco[5]移动到前面应该会产生这样的效果:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
// becomes
{5, 0, 1, 2, 3, 4, 6, 7, 8, 9}

编辑: 如果这些整数改为对象,会有什么不同吗?

2
非常感谢您宝贵的建议。 - Conner Ruhl
1
不,这是我正在进行的个人项目。我从未参加过编程课程。 - Conner Ruhl
你需要使用数组吗?你可以使用一个整数的ArrayList吗? - vcetinick
7个回答

7
  1. 将要移动的值存储在一个临时的int变量中
  2. 通过循环逐个向下复制数组中的所有元素
  3. 将存储在临时变量中的值分配给新位置(在本例中是数组的前面位置,taco[0])

无论是int值的数组还是对象数组,都没有任何区别。算法都是相同的。由于一个Object引用的数组实际上是32位或64位内存引用的数组,因此你实际上仍然在复制整数。

以下是可行的代码-有多种方法可以实现此目的,但这是基本思路:

public int[] moveValueAtIndexToFront(int[] arrayToBeShifted, int index) {
  int valueBeingMoved = originalArray[index];

  for (int i = index; i > 0; i--) {
    arrayToBeShifted[i] = arrayToBeShifted[i-1];
  }

  arrayToBeShifted[0] = valueBeingMoved;

  return arrayToBeShifted;
}
  • 此操作将把指定索引位置后面的所有值向后移动一个位置,并将被移动的值放到最前面。
  • 如果传入的索引是0,则不会有任何值被移动。
  • 如果传入的索引恰好是数组中最后一项的索引,则需要移动数组中的每个项目。如果你处理大型数组,并且在数组末尾进行了许多值的移动,则这将变得非常低效。

如果你好奇,你也可以查看arraycopy的源代码,通过OpenJDK项目


你会如何用代码实现这个?你会使用 System.arraycopy 吗? - Conner Ruhl
1
@ConnerRuhl - 除非你有令人信服的证据表明相反(例如,你已经对你的应用程序进行了分析,并且这段代码是一个明显的瓶颈),否则arraycopy的性能与简单循环的性能无关紧要。 - Stephen C
arraycopy还会分配额外的数组内存。这可能不是什么大问题,除非你处理非常大的数组或有其他理由认为这会成为一个问题。正如Stephen C所说,除非你在特定应用程序中有令人信服的证据,否则性能不应该成为一个问题。 - jefflunt
@normalocity - 实际上,System.arraycopy 不会分配任何内存。也许你把它和其他东西混淆了。 - Stephen C
嗯,也许是我错了。 - jefflunt

5
int[] taco = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int index = 5;
int temp = taco[index];
for(int i = index; i > 0; i--) {
    taco[i] = taco[i-1];
}
taco[0] = temp;

2
int[] taco = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int indexTarget = 7;
int valueAtIndex = taco[indexTarget];
for(int i = indexTarget; i > 0; i--){
   taco[i] = taco[i-1];
}
taco[0] = valueAtIndex;

1
对于新添加的问题:不,这并不会有任何影响,你只需要使用一个对象类型变量的临时变量,而不是 int 变量即可。

1
因为Java数组具有固定的大小,所以您应该创建一个新数组,并循环遍历其中的元素:
/**
 * Moves the element wanted to the front of the array, and returns a new array.
 * @param array The array to adjust
 * @param position The position of the element
 * @return The adjusted array
 */
private int[] moveElement (int[] array, int position)
{
    // Create temporary array to hold values
    int[] tempArray = new int[array.length];

    // Set first value to your wanted element
    tempArray[0] = array[position];

    // Set values of array before array[position]
    for (int tempPosition = 0; tempPosition < position; tempPosition++)
    {
        tempArray[tempPosition + 1] = array[tempPosition];
    }

    // Set values of array after array[position]
    for (int tempPosition = position + 1; tempPosition < array.length; tempPosition++)
    {
        tempArray[tempPosition] = array[tempPosition];
    }

    // Return newly created array
    return tempArray;
}

0
private int[] moveToZero (int[] workOnArray, int position) { 
 workOnArray[0]=workOnArray[position]+workOnArray[0];
 workOnArray[position]=workOnArray[0]-workOnArray[position];
 workOnArray[0]=workOnArray[0]-workOnArray[position];
 return workOnArray;
}

你交换了数组[0]和数组[index],但这并不符合本问题的要求。 - xuanyuanzhiyuan

0

我写了一个示例,你可以检查一下:

   public static void main(String[] args) {
        int[] taco = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int index = 2;
        int tmp = taco[0];
        int preValue;
        taco[0]=taco[index];
        for(int i=1,n=taco.length;i<n;i++){
            if(i==index+1)
                break;
            preValue = taco[i];
            taco[i]=tmp;
            tmp=preValue;
        }
        for(int i=0,n=taco.length;i<n;i++){
            System.out.println(taco[i]);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接