在2D阵列中移动某些元素

时间:2018-10-03 05:01:07

标签: java multidimensional-array

给出一个二维数组。如果为[i][0] == name,如何将其移至数组的最后一个元素?

String[][] array2d = [ [continents, name, Asia, Europe, Africa, Australia, South America, North America, Antartica],
 [profession, Teacher, Doctor, Lawyer],
 [brand, Apple, Samsung],
[name, Lisa, Peter, Sam, Jake],
[profession, Engineer, Professor, Dentist, Driver],
 [sex, value, Male, Female],
 [exp, value, 1, 2, 3, 4, 6]]

我想要的输出是

[ [continents, name, Asia, Europe, Africa, Australia, South America, North America, Antartica],
 [profession, Teacher, Doctor, Lawyer],
 [brand, Apple, Samsung],
 [profession, Engineer, Professor, Dentist, Driver],
 [sex, value, Male, Female],
 [exp, value, 1, 2, 3, 4, 6],
[name, Lisa, Peter, Sam, Jake]
]

下面是代码

 String[][] newArray =new String[array2d.length][];

     for (int i = 0; i < newArray.length; ++i) {
         newArray[i] = new String[array2d[i].length];
         for (int j = 0; j < newArray[i].length; ++j) {
             if (array2d[i][0] != "name") {
                 newArray[i][j] = array2d[i][j];
             }
         }
      }

但是,输出是

[[continents, name, Asia, Europe, Africa, Australia, South America, North America, Antartica],
 [profession, Teacher, Doctor, Lawyer],
 [brand, Apple, Samsung],
 [null, null, null, null, null],
 [profession, Engineer, Professor, Dentist, Driver],
 [sex, value, Male, Female],
 [exp, value, 1, 2, 3, 4, 6]]

谢谢!

3 个答案:

答案 0 :(得分:2)

首先备份最后一个元素(它是一个1d数组),然后将数组的最后一个元素深度复制到一个临时变量var中,然后将所选元素(它是一个1d数组)复制到该数组的最后一个元素。最后,将最后一个元素备份到阵列的所选元素中。 这是伪代码。

if(array[i][0] == name){  

   var = deepCopy(array[last]
   array[last] = deepCopy(array[i])
   array[i] = var

}

This link关于深度复制

答案 1 :(得分:2)

您可以通过这种方式尝试...您只需要重新排列数组引用

   package testProgram;

import java.util.Scanner;

public class TwoDArray {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String[][] array = new String[7][];
        array[0] = new String[] { "continents", "Asia", "Europe", "Africa", "Australia", "South America",
                "North America" };
        array[1] = new String[] { "profession", "teacher", "doctor", "lawyer" };
        array[2] = new String[] { "brand", "apple", "samsung" };
        array[3] = new String[] { "name", "lisa", "peter", "sam", "jack" };
        array[4] = new String[] { "profession", "engineer", "Professor", "Dentist", "Driver" };
        array[5] = new String[] { "sex", "value", "male", "female" };
        array[6] = new String[] { "exp", "value", "1", "2", "3", "4" };

        int index = sc.nextInt();
        //shuffling array references
        String[] tempReference = array[index - 1];
        for (int i = index - 1; i < array.length - 1; i++) {
            array[i] = array[i + 1];
        }
        array[array.length - 1] = tempReference;

        for (String[] tempArray : array) {
            for (String s : tempArray) {
                System.out.print(s + " ");
            }
            System.out.println();
        }
    }
}

答案 2 :(得分:1)

public static void main(String[] args) {

    //Initialize
    String[][] arr = new String[][]{
            new String[]{"continents", "abc"},
            new String[]{"name", "test"},
            new String[]{"something", "test something"},
    };

    int indexOfNameArray = -1;

    //Continuous swap logic
    for (int i = 0; i < arr.length; i++) {
        if (indexOfNameArray > -1 && indexOfNameArray < arr.length - 1) {
            String[] temp = arr[i];
            arr[i] = arr[i - 1];
            arr[i - 1] = temp;
            indexOfNameArray = i;
        } else if (arr[i][0].equals("name")) {
            indexOfNameArray = i;
        }
    }

    //To display output
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            System.out.print(arr[i][j] + ", ");
        }
        System.out.println();
    }
}