Append two 2D arrays without use of the Java arrays class - java

I'm confused on how to make a java method to append two different 2D arrays without using/importing the arrays class. This is what I have so far:
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
}
for (int i = 0; i < second.length; i++) {
for (int j = 0; j < second[i].length; j++) {
// don't know what to do here
}
}
return third;
}
A Cell is just an object, but I'm trying to understand the logic behind appending arrays so any help would be appreciated!
Also, I know there is a similar question found here (How do you append two 2D array in java properly?), but the answer is given on appending arrays from the top down (given both parameter arrays have the same amount of columns), while I am looking for appending arrays from side to side (assuming both parameter arrays have the same amount of rows).

You're almost there. I think something like this is what you're looking for.
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
for (int j = first[i].length; j < first[i].length + second[i].length; j++) {
third[i][j] = second[i][j-first[i].length];
}
}
return third;
}

Related

Cloning two-dimensional Arrays in Java [duplicate]

This question already has answers here:
copy a 2d array in java
(5 answers)
Closed 4 years ago.
I know that similar questions have been asked, but after reading their answers, I keep being unable to solbe my problem: I need to implement the Java method clone, which copies all the double entries in a given two-dimensional array a to a newly created two-dimensional array of the same type and size. This method takes the array a as input and returns the new array with the copied values.
IMPORTANT: I am not not allowed to use a library method to clone the array.
Here's what I've done so far: Maybe I didn't understand the requirements but it didn't work:
class Solution {
static double[][] clone(double[][] a) {
double[][] b = new double[a.length][a.length];
for (int i = 0; i < a.length; i++) {
b[i][i] = a[i][i];
}
return b;
}
}
This is the error message I get:
Status: Done
cloneLonger(weblab.UTest) failed: 'java.lang.AssertionError: expected:<3> but was:<2>'
Test score: 2/3
Something like this should work (with library method):
public class Util{
// clone two dimensional array
public static boolean[][] twoDimensionalArrayClone(boolean[][] a) {
boolean[][] b = new boolean[a.length][];
for (int i = 0; i < a.length; i++) {
b[i] = a[i].clone();
}
return b;
}
}
In this, your code has few mistakes. These corrections were done below. The two-dimension array has 2 lengths. In this case, you didn't consider inside array length.
class Solution {
static double[][] clone(double[][] a) {
double[][] b = new double[a[0].length][a.length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
b[i][j] = a[i][j];
}
}
return b;
}
}
you should iterate this array with two loops. This will helps you:
static double[][] clone(double[][] a) {
double[][] b = new double[a.length][];
for (int i = 0; i < a.length; i++) {
b[i]= new double[a[i].length];
for (int j = 0; j < a[i].length; j++)
b[i][j] = a[i][j];
}
return b;
}
You have some logical mistakes:
1. Matrix could be sized M x N where M is the number of rows and N is the number of columns. In your solution you are taking for granted that M is always equal to N.
2. You are iterating trough all the rows and there you do set only one column per row like target[K][K] = source[K][K] -> this will go copy the diagonal only and not the whole matrix.
Copy to a temp single row array and then assign it to the out array
static double[][] clone(double[][] a) {
double[][] b = new double[a.length][];
for (int i = 0; i < a.length; i++) {
double[] temp = new double[a[i].length];
for (int j = 0; j < temp.length; j++) {
temp[j] = a[i][j];
}
b[i] = temp;
}
return b;
}
Ever more "advanced" alternatives are:
static double[][] clone(double[][] a) {
double[][] b = new double[a.length][];
for (int i = 0; i < a.length; i++) {
b[i] = new double[a[i].length];
//for (int j = 0; j < a[i].length; ++j) {
// b[i][j] = a[i][
//}
System.arraycopy(a[i], 0, b[i], a[i].length);
}
return b;
}
Now there is a utility class Arrays worth knowing:
static double[][] clone(double[][] a) {
double[][] b = new double[a.length][];
for (int i = 0; i < a.length; i++) {
b[i] = Arrays.copyOf(a[i], 0, [a[i].length]);
}
return b;
}
And for primitive arrays the clone method still may be used. Cloning is not very pure, bypassing constructors, and might be dropped from java in some future.
static double[][] clone(double[][] a) {
double[][] b = new double[a.length][];
for (int i = 0; i < a.length; i++) {
b[i] = a[i].clone();
}
return b;
}
You can't create new array like that. If you are sure that length and width of array is same than only this will work.
class Solution {
static double[][] clone(double[][] a) {
boolean[][] b = new boolean[a.length][];
for (int i = 0; i < a.length; i++) {
b[i] = new double[a[i].length];
for (int j = 0; i < a[i].length; j++) {
b[i][j] = a[i][j];
}
}
return b;
}
}

duplicating a 2D array in a loop using basic commands

public static int[][] copyMatrix(int[][] matrix)
{
for (int i = 0; (i < matrix.length); i++)
{
int[][] duplicateMatrix = new int[matrix.length][matrix[i].length];
for (int j = 0; (j < matrix[i].length); j++)
{
duplicateMatrix[i][j] = matrix[i][j];
}
}
return duplicateMatrix;
}
hello all, this specific function doesnt seem to work since duplicateMatrix isnt initialized as a variable, but I cant seem to initialize since its being created in the loop, I cant find a way to generate the amount of cells need in a column.
help will be appreciated. thanks.
You should initialize the array before the loops, since you only want to initialize it once.
public static int[][] copyMatrix(int[][] matrix)
{
if (matrix.length < 1) {
return new int[0][0];
}
int[][] duplicateMatrix = new int[matrix.length][matrix[0].length];
for (int i = 0; (i < matrix.length); i++)
{
for (int j = 0; (j < matrix[i].length); j++)
{
duplicateMatrix[i][j] = matrix[i][j];
}
}
return duplicateMatrix;
}
This code assumes that all the rows in your input array have the same number of elements (which is true for matrices).
You can relax this assumption if you remember that a 2-dimentional array is simply an array of arrays :
public static int[][] copyMatrix(int[][] matrix)
{
int[][] duplicateMatrix = new int[matrix.length][];
for (int i = 0; (i < matrix.length); i++)
{
duplicateMatrix[i] = new int[matrix[i].length];
for (int j = 0; (j < matrix[i].length); j++)
{
duplicateMatrix[i][j] = matrix[i][j];
}
}
return duplicateMatrix;
}
A two-dimensional array is an array of arrays. You must first create the two-dimensional array, and then each one of its element individually:
public static int[][] copyMatrix(int[][] matrix)
{
int[][] duplicateMatrix = new int[matrix.length][];
for (int i = 0; (i < matrix.length); i++)
{
duplicateMatrix[i] = new int[matrix[i].length];
for (int j = 0; (j < matrix[i].length); j++)
{
duplicateMatrix[i][j] = matrix[i][j];
}
}
return duplicateMatrix;
}

Android game - Trying to create an array fails?

I am currently starting understanging Java. So while I've been trying to develop some minesweeper application I noticed, that when trying to add coordinates to a "Mines[]" array, the debugging window opens and my application doesn't continue displaying the purposed Minefield.
So that's my code:
package com.ochs.minesweeper;
public class MineField {
public Mine[] mines;
public MineField(int xMines, int yMines) {
mines = new Mine[xMines*yMines];
int xCounter = 0;
int yCounter = 0;
for(int i = 0; i < yMines; i++) {
for(int j = 0; j < xMines; j++) {
mines[i*j].setX(xCounter);
mines[i*j].setY(yCounter);
xCounter += 100;
}yCounter += 100;
}
}
}
Even when I just try something like:
for(int i = 0; i < xMines*yMines; i++) {
mines[i].setX(2);
}
or something like that it seems like I can't use the variable of the for-loop in my array to process...
Does anyone has an idea what I am doing wrong? I just want my MineField to have it's Mine[] array. This Mines are all created in the for loop with different coordinates so that they can be displayed in a grid on my surfaceview.
Does anyone has an idea? Or maybe another solution how I can create a simple grid of objects, in my example mines?
Thanks in advance!
Why not use 2 dimensional arrays? You can define Mine[][] mines and then in the loop:
for(int i = 0; i < yMines; i++) {
for(int j = 0; j < xMines; j++) {
mines[i][j].setX(xCounter);
mines[i][j].setY(yCounter);
xCounter += 100;
}yCounter += 100;
}
There is a problem with where you are setting the coordinates in this bit of code:
mines[i*j].setX(xCounter);
mines[i*j].setY(yCounter);
For example, the coordinates (x=2, y=3) and (x=3, y=2) refer to different locations on the grid, however 2*3=6 and 3*2=6. You need slightly more complicated logic to get a unique index for every coordinate (semihyagcioglu's approach is much better):
public MineField(int xMines, int yMines) {
mines = new Mine[xMines*yMines];
int xCounter = 0;
int yCounter = 0;
for(int i = 0; i < yMines; i++) {
for(int j = 0; j < xMines; j++) {
mines[i+(j*yMines)].setX(xCounter);
mines[i+(j*yMines)].setY(yCounter);
xCounter += 100;
}yCounter += 100;
}
}
The reason the application crashes is that you need to instantiate each mine object in your Mine[] array before trying to call setX() and setY() on them.
for (int i=0; i< (xMines*yMines); i++)
Mine[i] = new Mine();

Setting an ImageView from a For-Loop

This question may be a bit a tricky. I tried, but I am not really even sure what to search for, so I did not find any documentation on this.
I have a 7x10 grid, and each grid has its own image view. The ImageViews for the grids are named grid00, grid01, ..., grid79, grid710. I want to change the image for each grid in a nested for loop. so where it says in my code:
grid04.setImageResource(R.drawable.walllr);
What I realy want it to do is:
gridij.setImageResource(R.drawable.walllr);
where i and j after the word grid are the i and j from the nested for loop.
I am trying to change the image for all 70 items without writing code for each of the 70 items. Is this possible? Here is the code:
public void initialize() {
map = DM.getMap();
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j].isUsed()) {
grid04.setImageResource(R.drawable.walllr);
}
}
}
}
You don't mention where are those ImageViews named gridxx. If gridxx represents the id of that ImageView in the xml layout then you could use:
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j].isUsed()) {
int id = getIdentifier("grid" + i + j, "id", getPackageName());
((ImageView)findViewById(id)).setImageResource(R.drawable.walllr);
}
}
}
Keep in mind that the getIdentifier() method is a bit slow.
If for some odd reason, those are names used in the java class, the solution is to use an array like normal people.
Imageview[][] grid = new Imageview[7][10];
for(int i = 0; i <= 7; i++)
for(int j = 0; j <= 10; j++) //from 0 to 10, dunno if you want it from 1 to 10
{
grid[i][j]=new Imageview(....)
}
This is the only way to change all grids, like you want to do. First, create the grids. They will no longer be called grid04 or so, but grid[0][4].
You can create an array of ImageViews.
Imageview[] myImageViewArray = new ImageView[70];
int imageViewCounter=0;
and in your function
public void initialize() {
map = DM.getMap();
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j].isUsed()) {
myImageViewArray[imageViewCounter]=new ImageView();
myImageViewArray[imageViewCounter].setImageResource(R.drawable.walllr);
imageViewCounter++;
}
}
}
}
You can use this way. But you should modify it before you use for your function. Hope this helps.
Create an 2D array of ImageView[7][10].

size of Two-dimensional arrays in Java

I want to make a loop on Two-dimensional array in Java.
How I do that? I wrote:
for (int i = 0; i<=albums.size() - 1; i++){
for (int j = 0; j<=albums.size() - 1; j++){
But it didn't work. Thanks.
Arrays have a read-only field called length, not a method called size. A corrected loop looks like this:
for(int i = 0; i < albums.length; i++ ) {
for (int j = 0; j < albums[i].length; j++) {
element = albums[i][j];
You have to recognize that a 2-D array is just an array whose element type happens to be another array. So the i loop iterates over each element in albums (which is an array) and the j loop iterates over that child array (with a potentially different size).
A more transparent way would be like this:
String[][] albums;
for(int i = 0; i < albums.length; i++ ) {
String[] childArrayAtI = albums[i];
for (int j = 0; j < childArrayAtI.length; j++) {
String element = childArrayAtI[j];
}
}
Try this if you are working with Java 1.5+:
for(int [] album : albums) {
for(int albumNo : album) {
System.out.print(albumNo + ", ");
}
System.out.println();
}
First of all, a two-dimensional array looks like this in Java:
int[][] albums = new int[10][10];
Now, for iterating over it:
for (int i = 0; i < albums.length; i++) {
for (int j = 0; j < albums[i].length; j++) {
int value = albums[i][j];
}
}

Categories

Resources