如何在Java中移动数组中的位置? [英] How to move positions within an array in Java?

查看:92
本文介绍了如何在Java中移动数组中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个仅移动数组元素的程序。

两个变量: userInputVariable blankSpaceVariable

我有一个名为table的2D数组。定义为 table [userInputVariable +1] [6]

I have a 2D array named table. Defined as table[userInputVariable + 1][6]

我正在以表格格式打印此数组,

I am printing out this array in a table format, and the far left column is numbered by whatever number the user entered at the beginning of the program.

然后我问用户他们想在其中输入空格的地方是多少?数组。此空格就像数组中所有其他信息的分隔符一样。

I then ask the user where they would like to enter a blank space within the array. This blank space acts like a divider for all the other information in the array.

例如,如果用户在的开头输入10。 userInputVariable ,然后为空格输入5。一旦打印,数字应如下所示:

For example, if the user enters 10 at the start for the userInputVariable, and then enters 5 for the blank space. Once printed, the numbers should go like this:

1、2、3、4,-,5、6、7、8、9、10。

1, 2, 3, 4, --, 5, 6, 7, 8, 9, 10.

我的计划是创建一个for循环,并尝试将数组中的所有数字从空白变量开始移回一个位置。

My plan has been to create a for loop and try to move all the numbers in the array back a position starting from the blank space variable.

我当前拥有的东西,但不起作用:

What I currently have, but does not work:

for (int i = blankSpaceVariable; i < table.length - 1; i++) 
{
table[i] = table[i + 1];
}
table[blankSpaceVariable] = "--";

使用我当前的代码,数字如下:

With my current code, the numbers go like this:

1、2、3、4、6、7、8、9、10

1, 2, 3, 4, 6, 7, 8, 9, 10

也尝试通过几种不同的方式完成此操作,但其他信息我的2D数组中的数字没有移动。因此,我认为这种方法有望将2D数组中的所有信息向下移动,并为空白区域让位。

Tried completing this a few different ways also, but the other info within my 2D array didn't move with the numbers. So I thought that this approach can hopefully move all the info within my 2D array down, and make way for a blank section.

所有帮助都将不胜感激!

All help is greatly appreciated!

推荐答案

如果要将数字移到下一个位置,则应从数组的末尾开始。

If you want to move numbers to the next position, then you should start from the end of the array.

for (int i = table.length - 1; i > blankPosition; i--) {
    table[i] = table[i - 1];
}
table[blankPosition] = 99;

在您的示例中,可能有必要从 blankPosition中减去1。 code>(您说如果 blankPosition 为5,则表[4] -),但这在进入循环之前 可能会很好(这是为实际实现格式化数据的问题)。

From your example, it could be necesary to substract 1 from blankPosition (you say if blankPosition is 5, then the table[4] has the --), but this probably would be good to do before entering the loop (it's a matter of formatting the data for the actual implementation).

也许重命名也是个好主意。

And probably renaming table would be a good idea, too.

非常讨厌示例实现:

public class Arrays {

        public static void main(String[] args) {
                Integer blankPosition = Integer.parseInt(args[0]);
                // Using 99 instead of "--" because of array type
                int[] table = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 99};
                printEm(table);
                for (int i = table.length - 1; i > blankPosition; i--) {
                        table[i] = table[i - 1];
                }
                table[blankPosition] = 99;
                printEm(table);
        }

        public static void printEm(int[] array) {
                for(int i = 0; i < array.length; i++) {
                        System.out.print(array[i] + " ");
                }
                System.out.println();
        }
}

让我们运行它:

$ java Arrays 5
1 2 3 4 5 6 7 8 9 10 99 
1 2 3 4 5 99 6 7 8 9 10

这篇关于如何在Java中移动数组中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆