How to Split an Array into Multi Arrays in Java

Java - Split an array of arrays into multiple arrays

Following your logic...

Change this:

int[] zero = goal [1][];
int[] one = goal [2][];
int[] two = goal [3][];

to this:

int[] zero = goal [0];
int[] one = goal [1];
int[] two = goal [2];

java multi-dimensional array split

I hope I've understood correctly what you wanted to ask.

First of all as it was already mentioned you can't store integers in String array.

Secondly, in Java two-dimensional array is actually the array of arrays. So when you declare int[][] nums = int[4][3] you create an int[] array nums that has four elemets and each of these elements is another int[] array of length 3. So if you imagine that your two-dimentional array is kind of matrix you can easily retreive it's "rows" as elements of nums array.

int[][] nums = {{32, 123, 74}, {543, 98, 5}, {96, 24, 23}, {12, 98, 56}};

int[] rowOne = nums[0]; // {32, 123, 74}
int[] rowTwo = nums[1]; // {543, 98, 5}
int[] rowThree = nums[2]; // {96, 24, 23}
int[] rowFour = nums[3]; // {12, 98, 56}

Getting "columns" is a little bit trickier a long as they just don't exist in terms of java. But you still can do this as follows:

int[] columnOne = new int[nums.length];
for (int i = 0; i < columnOne.length; i++) {
columnOne[i] = nums[i][0]; // {32, 543, 96, 12}
}

int[] columnTwo = new int[nums.length];
for (int i = 0; i < columnTwo.length; i++) {
columnTwo[i] = nums[i][1]; // {123, 98, 24, 98}
}

int[] columnThree = new int[nums.length];
for (int i = 0; i < columnThree.length; i++) {
columnThree[i] = nums[i][2]; // {74, 5, 23, 56}
}

how to split a 2d array into multiple 2d arrays of different sized in java

In your case it's helpful to write down what you want:

         [0][0] [0][1] [1][0] [1][1]
[0][2] [0][3] [1][2] [1][3]
[0][4] [0][5] [1][4] [1][5]

[2][0] [2][1] [3][0] [3][1]
[2][2] [2][3] [3][2] [3][3]
[2][4] [2][5] [3][4] [3][5]

[4][0] [4][1] [5][0] [5][1]
[4][2] [4][3] [5][2] [5][3]
[4][4] [4][5] [5][4] [5][5]

Maybe you can already see a pattern here.
First, I created the inner loop for the first three lines:

for (int i = 0; i < 3; i++) {
int[][] array = new int[][]{
{pixels[0][i * 2], pixels[0][(i * 2) + 1]},
{pixels[1][i * 2], pixels[1][(i * 2) + 1]}
};
System.out.println(Arrays.deepToString(array));
}

As you see, it's always the zeroth and first row for the first three lines.
And in the next three lines, it's always the second and thrird row.
And the rule for all columns is that it's always zero, two, four and one, three, five.

So it comes to the nested loop:

for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
int[][] array = new int[][]{
{pixels[j * 2][i * 2], pixels[j * 2][(i * 2) + 1]},
{pixels[(j * 2) + 1][i * 2], pixels[(j * 2) + 1][(i * 2) + 1]}};
System.out.println(Arrays.deepToString(array));
}
}

Now you can store the created arrays for example in a list.

EDIT

Sorry, I think I made a mistake. This should do what you want:
create 6400 4x4 arrays. So you have to loop until the square root of the number of arrays you want(in this case 6400).
Do you have a possibility to check if the arrays are correct now?
Try this:

int vectorHeight = 4, vectorWidth = 4;
int numofVectors1 = (pixels.length * pixels.length) / (vectorHeight * vectorWidth);
ArrayList<int[][]> vectors = new ArrayList<>(numofVectors1);
int numberOfArrays = 6400;
for (int j = 0; j < Math.sqrt(numberOfArrays); j++) {
for (int i = 0; i < Math.sqrt(numberOfArrays); i++) {
int[][] array = new int[][]{
{
pixels[j * vectorHeight][i * vectorHeight],
pixels[j * vectorHeight][(i * vectorHeight) + 1],
pixels[j * vectorHeight][(i * vectorHeight) + 2],
pixels[j * vectorHeight][(i * vectorHeight) + 3]
},
{
pixels[(j * vectorHeight) + 1][i * vectorHeight],
pixels[(j * vectorHeight) + 1][(i * vectorHeight) + 1],
pixels[(j * vectorHeight) + 1][(i * vectorHeight) + 2],
pixels[(j * vectorHeight) + 1][(i * vectorHeight) + 3]
},
{
pixels[(j * vectorHeight) + 2][i * vectorHeight],
pixels[(j * vectorHeight) + 2][(i * vectorHeight) + 1],
pixels[(j * vectorHeight) + 2][(i * vectorHeight) + 2],
pixels[(j * vectorHeight) + 2][(i * vectorHeight) + 3]
},
{
pixels[(j * vectorHeight) + 3][i * vectorHeight],
pixels[(j * vectorHeight) + 3][(i * vectorHeight) + 1],
pixels[(j * vectorHeight) + 3][(i * vectorHeight) + 2],
pixels[(j * vectorHeight) + 3][(i * vectorHeight) + 3]
},
};
vectors.add(array);
}
}

java: How to split a 2d array into two 2d arrays

Lets say you have a 2d array of strings like so



String[][] array= new String[][]
{
{"a","b","c"},
{"d","e","f"},
{"h","i","j"},
{"k","l","m"}
};

Now you need a way to split these arrays at the half way point. Lets get the halfway point. Figure out how big the array is and then cut it in half. Note that you also must handle if the array is not an even length. Example, length of 3. If this is the case, we will use the Math.floor() function.



int arrayLength = array.length;
int halfWayPoint = Math.floor(arrayLength/2);
//we also need to know howmany elements are in the array
int numberOfElementsInArray = array[0].length;

Now we have all the info we need to create two 2d arrays from one. Now we must explicitly copy create and copy the data over.



//the length of the first array will be the half way point which we already have
String [][] newArrayA = new String[halfWayPoint][numberOfElementsInArray];
//this copies the data over
for(int i = 0; i < halfWayPoint; i++)
{
newArrayA[i] = array[i];
}

//now create the other array
int newArrayBLength = array.length - halfWayPoint;
String[][] newArrayB = new String[newArrayBLength][numberOfElementsInArray];
/*
* This copies the data over. Notice that the for loop starts a halfWayPoint.
* This is because this is where we left of copying in the first array.
*/
for(int i = halfWayPoint; i < array.length; i++)
{
newArrayB[i] = array[i];
}

And your done!

Now if you want to do it a little nicer, you could do it like this



int half = Math.floor(array/2);
int numberOfElementsInArray = array[0].length;
String [][] A = new String[half][numberOfElementsInArray];
String [][] B = new String[array.length - half][numberOfElementsInArray];
for(int i = 0; i < array.length; i++)
{
if(i < half)
{
A[i] = array[i];
}
else
{
B[i] = array[i];
}

}

And lastly, if you dont want to do it explicitly, you can use the built in functions. System.arraycopy() is one example. Here is a link to its api System.arraycopy()



int half = Math.floor(array/2);
int numberOfElementsInArray = array[0].length;
String [][] A = new String[half][numberOfElementsInArray];
String [][] B = new String[array.length - half][numberOfElementsInArray];
System.arraycopy(array,0,A,0,half);
System.arraycopy(array,half,B,0,array.length - half);

Java split sentence into a multi dimensional array

You mean something like this?

String[] names = new String[]{
"25 Jackson 11,693 Nevaeh 6,345",
"26 Jackson 44,444 Nevaeh 3,56"
};
String[][] words = new String[names.length][5];

for (int i = 0; i < names.length; i++) {
words[i] = names[i].split("\\s+");
}

Arrays.stream(words).map(Arrays::toString).forEach(System.out::println);

And why not use Collections instead of arrays?

List<String> names = Arrays.asList(
"25 Jackson 11,693 Nevaeh 6,345",
"26 Jackson 44,444 Nevaeh 3,56"
);

List<List<String>> words = names.stream()
.map(sentence -> sentence.split("\\s+"))
.map(Arrays::asList)
.collect(Collectors.toList());

words.forEach(System.out::println);

Iteratively split an array to subarrays according to a given length

It seems you want to create all subarrays of a given length from an array.

You can do it by hand:

static double[][] subArrays(double[] arr, int k)
{
double[][] subArrs = new double[arr.length-k+1][k];

for(int i=0; i<subArrs.length; i++)
for(int j=0; j<k; j++)
subArrs[i][j] = arr[i+j];

return subArrs;
}

Or use the built-in method Arrays.copyOfRange:

static double[][] subArrays(double[] arr, int k)
{
double[][] subArrs = new double[arr.length-k+1][];

for(int i=0; i<subArrs.length; i++)
subArrs[i] = Arrays.copyOfRange(arr, i, i+k);

return subArrs;
}

Test:

double[] arr = {2, 4, 6, 30, 9};

for(double[] subArr : subArrays(arr, 3))
System.out.println(Arrays.toString(subArr));

Output:

[2.0, 4.0, 6.0]
[4.0, 6.0, 30.0]
[6.0, 30.0, 9.0]


Related Topics



Leave a reply



Submit