Turning a 1-D Array into a 2-D Array - java

I am trying to take an array that contains 400 integers, and split it into a 20x20 2-D array. I thought I had the right algorithm, but the sum of the 1-D array does not match the sum of the 2-D array, so I’m obviously doing something wrong. Here is my code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
for (int z = 0; z < 400; z++)
{
array[x][y] = inArray[z];
}
}
}
}
What am I doing wrong?

For each pair of x and y, your code assigns every value from inArray to array[x][y]. This is clearly incorrect.
Here is one way to fix the code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0, z = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[z++];
}
}
}

You're assigning the last element in the input array to every element in the output array. The inner loop over the input array must be changed into a single assignment that picks the correct input element.
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[x * 20 + y]; // or devise your own calculation
}
}
}

The current approach will assign each element in the array with the last element in the original.
You should not iterate z but use it as a counter, in the same loops.
int z = 0;
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[z++];
}
}

You're basically assigning array[x][y] equal to inArray[0], then inArray[1], then inArray[2], all the way to [400].
I would do something like
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[x * 20 + y];
}
}
}

Related

2D Array flip vertically and horizontally

I have this method that's able to rotate the array by 90 degrees. I want to be to flip vertically and horizontally (I'm binding them with different buttons). Here's the method.
private void Rotate90() {
String[][] temp = new String[totalX][totalY];
for (int y = 0; y < totalY; y++) {
for (int x = 0; x < totalX; x++) {
temp[x][y] = fields[x][y].getText();
}
}
for (int y = 0; y < totalY; y++) {
for (int x = 0; x < totalX; x++) {
fields[x][y].setText(temp[y][x]);
}
}
Draw();
}
#khriskooper code contains an obvious bug: it flips array twice i.e. effectively does nothing. To flip array you should iterate only half of the indices. Try something like this:
private void flipHorizontally() {
for (int y = 0; y < totalY; y++) {
for (int x = 0; x < totalX/2; x++) {
String tmp = fields[totalX-x-1][y].getText();
fields[totalX-x-1][y].setText(fields[x][y].getText());
fields[x][y].setText(tmp);
}
}
}
private void flipVertically() {
for (int x = 0; x < totalX; x++) {
for (int y = 0; y < totalY/2; y++) {
String tmp = fields[x][totalY - y - 1].getText();
fields[x][totalY - y - 1].setText(fields[x][y].getText());
fields[x][y].setText(tmp);
}
}
}

How would i iterate over a 2d array of varying size in Java?

I'm currently creating a brickbreaker (or breakout, like atari breakout) and for the bricks im using a 2d array:
private Brick bricks[][] = new Brick[10][41];
To destroy a specific Brick (for whatever reason i have to destroy that), i created a method:
public void destroyBrick(Brick brick) {
for(int x = 0; x < bricks[x].length; x++) {
for(int y = 0; y < bricks[y].length; y++) {
if (bricks[x][y].equals(brick)) {
bricks[x][y] = null;
}
}
}
}
The way the array is created is the following:
for(int j=0; j<bricks[0].length; j++){
for(int i=0; i<bricks.length; i++){
bricks[i][j]=new Brick(game,10,Color.GREEN,j*margin,i*margin);
}
}
Now for some reason the counter for x eventually reaches 10 and the counter for y reaches 0 where it crashes, and i can't figure out why.
Is there a different way of iterating a 2d array that has different values for columns and rows?
note
The reason the formatting and my spelling is so rubbish is, that im writing this on the app version. Feel free to correct any mistakes or formatting errors, before i can.
You are referencing y in the loop that iterates on it. This compiles correctly, because y is defined at the point of reference, but it does not do what you need. In addition, the outer loop should stop upon reaching bricks.length, not bricks[x].length.
These two lines
for(int x = 0; x < bricks[x].length; x++) {
for(int y = 0; y < bricks[y].length; y++) {
should be
for(int x = 0; x < bricks.length; x++) {
// ^
for(int y = 0; y < bricks[x].length; y++) {
// ^
The first int x doesn't refer to what you need, I think.
x ranges from 0 to the length of the array indexed with x, that is 41.
for (int x = 0; x < bricks.length; x++)
for (int y = 0; y < bricks[x].length; y++)
Should work (at least I wrote this without testing)
Try iterating with any of this
public void destroyBrick(Brick brick) {
for(int x = 0; x < bricks.length; x++) {
for(int y = 0; y < bricks[0].length; y++) {
if (bricks[x][y].equals(brick)) {
bricks[x][y] = null;
}
}
}
}
or
public void destroyBrick(Brick brick) {
for(int x = 0; x < bricks.length; x++) {
for(int y = 0; y < bricks[x].length; y++) {
if (bricks[x][y].equals(brick)) {
bricks[x][y] = null;
}
}
}
}
if there's any other error, use your debugger to find out where exactly

Indexing string for insertion into char[][][] array

I'm a little stuck on the arithmetic for this program. I have a string theString that has a length of x*y*z. x, y, and z are the dimensions of a char[][][] array (lets call it cArr).
I want to insert the characters of theString into cArr based on a specific index. Here's an example of a 5x4x3 array.
String theString = someMethod(); //returns a string of length 60
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(/*~~~*/);
}
}
}
I can figure out the arithmetic to insert characters into a char[5][4] array from a string of length 20:
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y] = theString.charAt((5*y)+x);
}
}
What arithmetic should I be using in the charAt() method for a 3-dimensional char array?
My best attempt has been:
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(((3^2)*z)+(4*y)+x);
}
}
}
But this makes all z-index layers of cArr the same.
The rule is simple for problems like this. You start from the innermost loop and proceed up with x + y*(length of x) + z*(length of y)*(length of x).
In this example, it would be,
cArr[x][y][z] = theString.charAt(x + y*5 + z*4*5);

How can I give a certain row of elements all the same value? Using a 2d array?

If I have
String[][] trenches = new String[10][10];
How can i make all elements in row 0 have the value of "X" ?
or all elements in row 1 have the value of "0" ?
You can do this using a for loop.
int row = 0;
int value = 5;
for(int i = 0; i < trenches[row].length(); i ++)
{
tenches[row][i] = value;
}
You could put this in a function and pass the row and value
public void standardiseRow(int row, int value)
Think of your 2d array as an x and y coordinate system. You can have a double for loop that goes through your x multiple times, but goes through your y only one time. In this case, your outer loop would be y and your inner loop would be x.
for (int y = 0; y < array.length; y++) {
for (int x = 0; x < array.length; x++) {
if (y == 0) {
array[x][y] = X;
}
if (y == 1) {
array[x][y] = 0;
}
}
}
Try this code:
String[][] trenches = new String[10][10];
trenches[0] = StringUtils.repeat("X", 10).split("");
trenches[1] = StringUtils.repeat("0", 10).split("");

Average value of bytes

I have such a problem. I want to make "average" image from the few diffirent. my idea was to load them to BufferImage sequentially, sum values in each bit, and than just divide by number of photos. The problem is that i can't divide byte[][] by number. What am i doing wrong, or what is the proper way to do this?
File[] others = getOtherImageFiles(file);
byte[][] pixels = new byte[300][];
List<byte[][]> pixelsList = new ArrayList<byte[][]>();
for (int i = 0; i < others.length; i++) {
try {
img = ImageIO.read(others[i]);
} catch (IOException e) {
}
for (int x = 0; x < img.getWidth(); x++) {
pixels[x] = new byte[img.getHeight()];
for (int y = 0; y < img.getHeight(); y++) {
pixels[x][y] = (byte) (img.getRGB(x, y));
}
}
pixelsList.add(pixels);
}
byte[][] pixelsSum = new byte[300][];
for (int i = 0; i < pixelsList.size(); i++) {
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pixelsSum[x][y] += pixelsList.get(i)[x][y];
}
}
}
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pixelsSum[x][y]/10;
}
}
}
i want to use them as a face comparer and the point is to create a scheme of faces.
You're close. You did the division by 10, but you never wrote the result back into the pixelsSum array, so the result is lost. You need to do:
pixelsSum[x][y] = pixelsSum[x][y] / 10;
or in shorthand:
pixelsSum[x][y] /= 10;
Another problem you will run into is that each byte in Java (or any other language) can only hold values between 0 and 255. So if you have a bytes x = 255, y = 1, x + y will turn out to be 0. When you add up 10 images, it will almost certainly go over the 255 limit. I suggest changing pixelsSum from byte[][] to int[][], and then turn it back to bytes after you do the division.
Instead of this set of loops:
for (int i = 0; i < pixelsList.size(); i++) {
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pixelsSum[x][y] += pixelsList.get(i)[x][y];
}
}
}
you should put the pixelsList reference in the middle:
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
for (int i = 0; i < pixelsList.size(); i++) {
pixelsSum[x][y] += pixelsList.get(i)[x][y];
}
pixelsSum[x][y] /= pixelsList.size();
}
}
This should calculate the average immediately rather than looping through the pixelsSum array again. Note also the use of the /= operator which performs both division and assignment compared with the / operator which is limited to division in the context of numbers.

Categories