如何在java中移动数组中的元素?

如何在java中移动数组中的元素?,java,arrays,move,Java,Arrays,Move,大家好,我正在学习java,我想知道如何将数组中的元素从位置X移动到位置Y,而不考虑X和Y在数组中的位置。我还想通过移动元素来关闭数组中留下的洞。 谢谢。此解决方案有两个假设:1)X和Y都小于阵列的长度,2)孔仅是移动的一个值/槽 int a[] = new int[]; int x; int y; // Populate a, x, and y int b[] = new int[a.length - 1]; int j = 0; for (int i = 0; i < a.len

大家好,我正在学习java,我想知道如何将数组中的元素从位置X移动到位置Y,而不考虑X和Y在数组中的位置。我还想通过移动元素来关闭数组中留下的洞。
谢谢。

此解决方案有两个假设:1)X和Y都小于阵列的长度,2)孔仅是移动的一个值/槽

int a[] = new int[];
int x;
int y;

// Populate a, x, and y

int b[] = new int[a.length - 1];
int j = 0;

for (int i = 0; i < a.length; i++)
{
  if (i == x)
    continue;
  else if (i == y)
    b[j] = a[x];
  j++;
}
inta[]=新的int[];
int x;
int-y;
//填充a、x和y
int b[]=新int[a.长度-1];
int j=0;
for(int i=0;i
在这种情况下,最好使用
ArrayList
,而不是数组。但到目前为止,你做了哪些尝试?您是否有一些代码要共享,哪些代码出了问题/给了您麻烦?元素位于Y“位置”时会发生什么?消失?