How to reorder the columns of a 2d array? - java

Is there a simple way to randomize the order of columns in a 2d array? For example could I use it to randomize the columns of this 2d array
int[][] dataArray = {{1, 1, 0, 1, 0, 0},
{0, 0, 1, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1}};
I have been trying for awhile with little luck so far.
What i am trying to do is make columns 1 2 3 4 5 6 randomly become columns 2 3 1 6 5 4 for example.
I have tried randomly drawing numbers and trying to make the columns match the order but i can't seem to get that working.

You could always construct a 1D array, randomize its contents, then reconstruct a 2D array.
int[] newArray = new int[a.length + b.length + c.length + d.length];
// where a, b, c, and d are the smaller arrays inside dataArray
System.arrayCopy(a, 0, newArray, 0, a.length);
System.arrayCopy(b, 0, newArray, a.length, b.length);
System.arrayCopy(c, 0, newArray, a.length + b.length, c.length);
System.arrayCopy(d, 0, newArray, a.length + b.length + c.length, d.length);
This will construct a 1D array called newArray that has the contents of all the smaller arrays.
Now randomize that array as shown here.
Then to reconstruct the original array:
int newArray2d[][] = new int[5][4]; //do this dynamically based on the size of the first array.
for(int i=0; i<5;i++)
for(int j=0;j<4;j++)
array2d[i][j] = array1d[(j*10) + i];

Related

When building array program in Java, it does not return the last number of the array when following the array.length - 1 method

I am writing a small program to take an input array, nums and create a new array that is double the size and returns the last number from the previous array as the last and only different number in the new array.
When I try to return this number using the nums.length - 1 formula to get the end of the array, it returns the number in the middle of the new array. Below is my program.
public int[] makeLast(int[] nums) {
int lengonewarr = nums.length;
int officiallength = lengonewarr * 2;
int [] makezero = new int [officiallength];
makezero [nums.length-1] = nums[nums.length-1];
return(makezero);
}
These are the outputs I should be making:
makeLast([4, 5, 6]) → [0, 0, 0, 0, 0, 6]
makeLast([1, 2]) → [0, 0, 0, 2]
makeLast([3]) → [0, 3]
But I instead get something like:
makeLast([1, 2, 3, 4]) → [0, 0, 0, 0, 0, 0, 0, 4] (My output) [0, 0, 0, 4, 0, 0, 0, 0]
makeLast([2, 4]) → [0, 0, 0, 4] (My output) [0, 4, 0, 0]
Any advice is greatly appreciated.
the array makezero's length is officiallength, not nums.length, just replace the first nums.length by makezero.length or officiallength.
makezero [makezero.length -1] = nums[nums.length-1];
BTW: It's better to name the variable with camelCase style, which will make the code more readable. Like makeZero or officialLength
I modified your and here it is:
public int[] makeLast(int[] nums) {
int lengonewarr = nums.length;
int officiallength = lengonewarr * 2;
int [] makezero = new int [officiallength];
makezero [makezero.length-1] = nums[nums.length-1];
return(makezero);
}
As the array makezero is double size, the length of lengonewarr is always in the middle of makezero.

Moving array elements to the left

Writing a code that shifts array elements left by an int n (keyboard input) and replaces space with zeros.
Ex. int[] data = { -1, 1, 3, 7, 5} and I want to see { 3, 7, 5, 0, 0}.
Suppose I have:
public static void shiftLeft( int[] data, int n )
{
System.arraycopy( data, 1, data, 0, data.length - 1 );
data[data.length - 1] = 0;
}
Any quick help would be great.
You were close, but your srcPos argument should be n and the length argument should be data.length - n. You can then use the overloaded Arrays#fill method that accepts a start/end index to fill the rest of the array with 0:
public static void shiftLeft(int[] data, int n) {
System.arraycopy(data, n, data, 0, data.length - n);
Arrays.fill(data, data.length - n, data.length, 0);
}
Testing this with your example input yields the following:
int[] data = { -1, 1, 3, 7, 5};
shiftLeft(data, 2);
System.out.println(Arrays.toString(data));
Output:
[3, 7, 5, 0, 0]

Simple Java array syntax which i'm confused over

public static void main(String[] args) {
int[][] b = {{0, 0, 0, 1},
{0, 0, 1, 1},
{0, 1, 1, 1}};
int[] u = new int[b.length];
for (int i = 0; i < u.length; i++) {
for (int j = 0; j < b[i].length; j++) {
u[i] = u[i] +b[i][j];
}
System.out.println(u[i]);
}
}
What is the differences between written b[i].length; and b.length;
When i run this code with b[i].length; the output is 1,2,3.
When running with b.length; gives the output 0,1,2
Your array got 2 dimensions, thus it is an array of an array. If you use b.length you will get the amount of arrays that are stored within b
Your array could look like this:
int[][] b = {{1}, {1,2}, {1,2,3}};
So b is an array that contains 3 other arrays. b.length (in this case) will always be 3. If you use b[n].length, you will get the length of the array with index n within b. In my example above: b[0].length will be 1, b[1].length will be 2, b[2].length will be 3.
b.length gives you the length of array b which is 3 because it has three element: b[0] which is an array {0, 0, 0, 1}, b[1] which is an array {0, 0, 1, 1} and finally b[2] which is an array {0, 1, 1, 1}.
b[i].lenght gives you the length of element b[i] which is 4. The reason that you can use length method in b[i] is because b[i] is also array (b is an array of arrays so every element of b is an array of int's).
b[i].length will return 4 for every i because every element b[i] is an array of length 4 .However, note that b[i] doesn't need to have same length
for example if you had:
int[][] b = {{0, 0, 0, 1},{1, 1}};
Then here b.length returns you 2, b[0].length returns 4 and b[1].length returns 2.
In your code your have a 2d array.
/*This is an array of arrays*/
int[][] b = {
{0, 0, 0, 1},
{0, 0, 1, 1},
{0, 1, 1, 1}
};
This is an array to arrays.
b.length will return the amount of arrays in the main array
b[i].length will return the amount of elements in one specific array on index i
b.length return number all rows but b[i].length; return number column for row i
b.length is the number of rows in your matrix
b[i].length is the number of elements i-th row
b refers to an array of arrays.
b.length returns how many elements b has - in other words, how many "nested" arrays you have.
b[i].length returns how many elements b[i] has - in other words, the length of the nested array referred to by b[i].

How to store a 2D array in a regular array? Java

basically I want to be able to store a 2D array such as this
int [][] preferredMoves = {
{0,0}, {0, arrLength}, {length%2, arrLength},
{0, length%2}, {arrLength, 0}, {0, length%2},
{arrLength, arrLength}, {length%2, length%2},
{arrLength, length%2}, {length%2, 0}
};
In a single
int [] moves;
array.
I'm sure this is possible since I'm just storing a list..., but I can't seem to find information on this anywhere... or maybe its not possible?
EDIT
I am dealing with matrices.
I want to store the list in a single array to then return that array to use it elsewhere.
So then every time I call it, all I have to do is something like this...
int row = Computer.moves()[0];
int col = Computer.moves()[1];
I also need to loop through that single array, which contains the 2D array multiple times..
Not sure if this is what you meant,
but you could drop the internal { ... } to convert this to a one-dimensional array:
int [] moves = {
0, 0, 0, arrLength, length % 2, arrLength,
0, length % 2, arrLength, 0, 0, length % 2,
arrLength, arrLength, length % 2, length % 2,
arrLength, length % 2, length % 2, 0
};
And you can translate 2D indexes (i, j) to 1D index k using the formula:
k = i * 2 + j;
Janos' answer is probably what you want. Alternatively you could create a class, e.g. Pair, and store it in a Pair[] array.

Find array indices

I'm sure there is a CS term for what I'm trying to accomplish but I'm not sure what it is. I have three arrays, let's call them a, b, and c. I am iterating through every possible combination of the arrays, which would be a*b*c iterations.
I am passing a function an int of the current iteration (such that iteration goes from 0 to a*b*c-1) and the length of a, b, and c. I want that function to be able to print out every unique permutation of indices, calculated from just the iteration count and the lengths of a, b, and c.
This is what I have now:
class Test {
public static void printIndices(int i, int a, int b, int c) {
System.out.println(i%a + ", " + (i+1)%b + ", " + (i+2)%c);
}
public static void main(String[] args) {
int a[] = new int[2];
int b[] = new int[2];
int c[] = new int[3];
int iterations = a.length * b.length * c.length;
for (int i=0; i < iterations; i++){
printIndices(i, a.length, b.length, c.length);
}
}
}
It generates this output:
0, 1, 2
1, 0, 0
0, 1, 1
1, 0, 2
0, 1, 0
1, 0, 1
0, 1, 2
1, 0, 0
0, 1, 1
1, 0, 2
0, 1, 0
1, 0, 1
As you can see there are duplicates. I want the output to be:
0, 0, 0
1, 0, 0
0, 1, 0
1, 1, 0
0, 0, 1
1, 0, 1
0, 1, 1
1, 1, 1
0, 0, 2
1, 0, 2
0, 1, 2
1, 1, 2
(the order is unimportant, as long as every permutation is there with no duplicates).
Obviously my output line is wrong:
System.out.println(i%a + ", " + (i+1)%b + ", " + (i+2)%c);
What are the correct operations to get the output I am looking for?
I understand that this code is a bit silly looking and it's not at all what I'm actually doing, but it demonstrates the case well.
As mentioned in the comments you are looking for the Cartesian product.
Your approach with modular arithmetic almost works. You need only a few modifications to get the correct result:
System.out.println(i%a + ", " + (i/a)%b + ", " + (i/a/b)%c);
See it working online: ideone

Categories