Java 在数组中拆分和移动项目

Java 在数组中拆分和移动项目,java,Java,我有一个包含整数的数组 我要做的是在某个位置拆分数组,并将该位置前面的所有项目移动到数组的末尾,然后将该位置后面的项目移动到数组的前面 有人知道怎么做吗?替换 cards[cards.length - position + i] = cards[i]; 与 既然已经指出了错误,那么应该很清楚哪里出了错 干杯 更换 cards[cards.length - position + i] = cards[i]; 与 既然已经指出了错误,那么应该很清楚哪里出了错 干杯 使用一个for循环简化代码:

我有一个包含整数的数组

我要做的是在某个位置拆分数组,并将该位置前面的所有项目移动到数组的末尾,然后将该位置后面的项目移动到数组的前面

有人知道怎么做吗?

替换

cards[cards.length - position + i] = cards[i];

既然已经指出了错误,那么应该很清楚哪里出了错

干杯

更换

cards[cards.length - position + i] = cards[i];

既然已经指出了错误,那么应该很清楚哪里出了错


干杯

使用一个
for
循环简化代码:

for (int i = 0; i < position && position + 1 + i < cards.length; i++) {
    swapCard(cards, i, position + 1 + i);
}
如果
cards
是全局的,修改
swapCard
void swapCard(int x,int y)
并在
for
循环中,只需调用
swapCard(i,位置+1+i)

@埃里克·G·哈格斯特罗姆:这就是我的想法:

position: 0  1  2  3  4
   array: 1  3  5  7  9
如果
position=1
,则表示
array[1]=3
仍然存在,数组将更改为:

position: 0  1  2  3  4
   array: 5  3  1  7  9
position: 0  1  2  3  4
   array: 5  7  9  3  1
如果
位置=2

position: 0  1  2  3  4
   array: 7  9  5  1  3
如果位置=3:

position: 0  1  2  3  4
   array: 9  3  5  7  1
但我错了。他们希望阵列更改为:

position: 0  1  2  3  4
   array: 5  3  1  7  9
position: 0  1  2  3  4
   array: 5  7  9  3  1
其中
位置=1
,并且:

position: 0  1  2  3  4
   array: 9  7  1  3  5
其中
position=3


顺便说一句,在您的示例中,我的方法的结果是:
3,2,1

使用一个
简化代码,用于
循环:

for (int i = 0; i < position && position + 1 + i < cards.length; i++) {
    swapCard(cards, i, position + 1 + i);
}
如果
cards
是全局的,修改
swapCard
void swapCard(int x,int y)
并在
for
循环中,只需调用
swapCard(i,位置+1+i)

@埃里克·G·哈格斯特罗姆:这就是我的想法:

position: 0  1  2  3  4
   array: 1  3  5  7  9
如果
position=1
,则表示
array[1]=3
仍然存在,数组将更改为:

position: 0  1  2  3  4
   array: 5  3  1  7  9
position: 0  1  2  3  4
   array: 5  7  9  3  1
如果
位置=2

position: 0  1  2  3  4
   array: 7  9  5  1  3
如果位置=3:

position: 0  1  2  3  4
   array: 9  3  5  7  1
但我错了。他们希望阵列更改为:

position: 0  1  2  3  4
   array: 5  3  1  7  9
position: 0  1  2  3  4
   array: 5  7  9  3  1
其中
位置=1
,并且:

position: 0  1  2  3  4
   array: 9  7  1  3  5
其中
position=3


顺便说一句,在你的例子中,我的方法的结果是:
3,2,1

不要重新发明轮子

使用。。。他们是为了这样一种逻辑而存在的,使得操作更加容易

public static void main(String[] args) {
        //define the maze
        String[] array = { "A", "B", "C", "D", "1", "2", "3", "4" };
        List<String> listA = new ArrayList<>(Arrays.asList(array));
        System.out.println(listA);
        // this will print [A, B, C, D, 1, 2, 3, 4]

        // then add the lower half of the cards maze
        List<String> cutList = new ArrayList<>(listA.subList(listA.size() / 2, listA.size()));
        System.out.println(cutList);
        // this will print [1, 2, 3, 4]

        // then add the upper half of the cards maze
        cutList.addAll(listA.subList(0, listA.size() / 2));
        // this will print [1, 2, 3, 4, A, B, C, D]
        System.out.println(cutList);
    }
publicstaticvoidmain(字符串[]args){
//定义迷宫
字符串[]数组={“A”、“B”、“C”、“D”、“1”、“2”、“3”、“4”};
List listA=newarraylist(Arrays.asList(array));
System.out.println(listA);
//这将打印[A、B、C、D、1、2、3、4]
//然后添加下半部分的卡迷宫
List cutList=newarraylist(listA.subList(listA.size()/2,listA.size());
系统输出打印LN(切割列表);
//这将打印[1,2,3,4]
//然后添加卡迷宫的上半部分
addAll(listA.subList(0,listA.size()/2));
//这将打印[1、2、3、4、A、B、C、D]
系统输出打印LN(切割列表);
}

不要再发明轮子

使用。。。他们是为了这样一种逻辑而存在的,使得操作更加容易

public static void main(String[] args) {
        //define the maze
        String[] array = { "A", "B", "C", "D", "1", "2", "3", "4" };
        List<String> listA = new ArrayList<>(Arrays.asList(array));
        System.out.println(listA);
        // this will print [A, B, C, D, 1, 2, 3, 4]

        // then add the lower half of the cards maze
        List<String> cutList = new ArrayList<>(listA.subList(listA.size() / 2, listA.size()));
        System.out.println(cutList);
        // this will print [1, 2, 3, 4]

        // then add the upper half of the cards maze
        cutList.addAll(listA.subList(0, listA.size() / 2));
        // this will print [1, 2, 3, 4, A, B, C, D]
        System.out.println(cutList);
    }
publicstaticvoidmain(字符串[]args){
//定义迷宫
字符串[]数组={“A”、“B”、“C”、“D”、“1”、“2”、“3”、“4”};
List listA=newarraylist(Arrays.asList(array));
System.out.println(listA);
//这将打印[A、B、C、D、1、2、3、4]
//然后添加下半部分的卡迷宫
List cutList=newarraylist(listA.subList(listA.size()/2,listA.size());
系统输出打印LN(切割列表);
//这将打印[1,2,3,4]
//然后添加卡迷宫的上半部分
addAll(listA.subList(0,listA.size()/2));
//这将打印[1、2、3、4、A、B、C、D]
系统输出打印LN(切割列表);
}

使用3张卡1、2、3和位置1尝试此操作。结果应该是2,3,1。你的方法得到2,1,3。OP没有要求交换,他们要求整个阵列移位。这是我的错误!但在您的示例中,我的方法的结果是:
3,2,1
尝试使用3张卡1,2,3和位置1。结果应该是2,3,1。你的方法得到2,1,3。OP没有要求交换,他们要求整个阵列移位。这是我的错误!但在您的示例中,我的方法的结果是:
3,2,1
Awesome!这正是我需要的。谢谢你向我指出这个错误!令人惊叹的!这正是我需要的。谢谢你向我指出这个错误!