Reputation: 35
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!
Upvotes: 1
Views: 93
Reputation: 157
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();
}
}
}
Upvotes: 2
Reputation: 7917
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();
}
}
Upvotes: 1
Reputation: 1648
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
Upvotes: 2