1

Given a 2d array. If [i][0] == name, how do I move it to last element of the array?

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]]

My desired output is

[ [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]
]

Below are the codes

 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];
             }
         }
      }

However, the output is

[[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]]

Thank you!

5
  • what have you tried? please post your code.. Commented Oct 3, 2018 at 5:02
  • can you post your code which you tried to perform the mentioned operation? Commented Oct 3, 2018 at 5:02
  • check if [i][0] == name if it is then exchange it with the last element and keep one pointer for last element decrease this pointer every time you exchange one element Commented Oct 3, 2018 at 5:23
  • @Kartik I have posted the code. Please take a look. Thanks! Commented Oct 3, 2018 at 5:36
  • @JavaLearner1 I have posted the code. Please take a look. Thanks! Commented Oct 3, 2018 at 5:36

3 Answers 3

2

First backup last element ( it is a 1d array) ,deep copy your arrays last element to a tempary variable as var) then copy your selected ( it is a 1d array )element to the last element of the array. At last copy backed up last element to the array's your selected element. this is the pseudo code.

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

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

}

This link is about deep Copy

Sign up to request clarification or add additional context in comments.

4 Comments

the pseudo code given is applicable to 1d array? What I have is a 2d array, should I convert to 1d array?
No your 2d arrays selected element is 1d array. that's what i mean.
this can be done after selecting the element in 2d array (which is a 1d array).
in your case arrays type is String so var is a type of String [ ], eg : - String [ ] var = array2d[i]
2

You can try in this way... You only need to rearrange array references

   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 Comments

You are assuming that "name" will always be at index 3. Try writing the code again without hard-coding array[3].
now you are asking the user for the index.. this is still hard-coding.. as the OP mentioned, you need to traverse the array to look for an item where [i][0] == name
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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.