在java中,根据索引号将元素移动到数组前面
假设我有一个数组:在java中,根据索引号将元素移动到数组前面,java,arrays,Java,Arrays,假设我有一个数组: 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} 编辑: 如果INT是对象,会有区别吗 将要移动的值存储在临时int变量中 将数组中的所有元素复制到循环中,一次复制一个 将存储在临时变量中的值分配到新位
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}
编辑:
如果INT是对象,会有区别吗
int
变量中taco[0]
)int
值或对象的数组,则不会产生任何差异。算法是一样的。由于对象
引用的数组实际上是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
的源代码。因为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;
}
/**
*将所需元素移动到数组的前面,并返回一个新数组。
*@param array要调整的数组
*@param position元素的位置
*@返回调整后的数组
*/
私有int[]移动元素(int[]数组,int位置)
{
//创建临时数组以保存值
int[]tempArray=newint[array.length];
//将第一个值设置为所需元素
tempArray[0]=数组[位置];
//在数组[位置]之前设置数组的值
对于(int-tempPosition=0;tempPosition
对于新添加的问题:不,这没有任何区别,您只需要使用对象类型变量的临时变量而不是int变量。我编写了一个示例,您可以检查它:
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;
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]);
}
}
publicstaticvoidmain(字符串[]args){
int[]taco={0,1,2,3,4,5,6,7,8,9};
int指数=2;
int tmp=taco[0];
int-preValue;
塔可[0]=塔可[指数];
对于(int i=1,n=taco.length;我感谢你的宝贵意见。不,这是我正在从事的一个个人项目。我从来没有上过编程课程。你需要使用数组吗?你能使用整数数组列表吗?你会如何处理代码?你会实现System.arraycopy吗?@ConnerRuhl-除非你有令人信服的证据向公司证明Ntry(例如,您已经分析了您的应用程序,而此代码是一个明显的瓶颈)与简单循环相比,arraycopy
的性能无关紧要。arraycopy还分配了额外的数组内存。这可能不是什么大问题,除非您处理的是非常大的数组,或者有其他理由相信这会成为一个问题。正如Stephen C所说,除非您有令人信服的证据证明特定的应用程序,性能在这里不应该是一个问题。@normalocity-实际上,System.arraycopy
不分配任何内存。也许你把它与其他东西混淆了。嗯,也许是我。我的错误。你交换了数组[0]和数组[index],但它不适合这个问题。
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;
}
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]);
}
}