How to Equal Two 2D Arrays Together in Java

How to equal two 2d arrays together in java?

Here's my solution.
You have confused some code lines, putting them in the while block, and forgot the doubleFor during the 2dArray population. In the following code, you can work directly on main2dArray.

First of all,

private static String [][] main2dArray= null;

Then, in create2d(), after the file reading, add all the splitter in arrayL:

       ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
String[] splitter= line.split(" ");
arrayL.add(splitter);
}

Now you can copy all the values in main2dArray, with a double-For:

       main2dArray= new String[arrayL.size()][arrayL.get(0).length];
for(int i = 0; i<main2dArray.length; i++) {
for(int j=0; j<main2dArray[0].length;j++)
main2dArray[i][j] = arrayL.get(i)[j];
}

so, when you call array.create2d(); (i don't have modified it), the output is:

[[25, S, 92, 78, 2], [14, 21, 52, 2, 4], [2, 61, 11, 24, 7], [56, 45, 16, 57, 4], [32, 15, 98, 13, 6]]

with the char "S" in the right position!

To avoid misunderstanding, here is the complete code with comments:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Ciao {
private static String [][] main2dArray= null;

public static void main(String[] args) throws IOException{
array.create2d(); //(calls out create2d)
addStartPosition();
}

private static void create2d(){
String line;
try {
FileReader fr= new FileReader("data.txt");
BufferedReader br= new BufferedReader(fr);

ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
String[] splitter= line.split(" ");
arrayL.add(splitter);
}//while

main2dArray= new String[arrayL.size()][arrayL.get(0).length];
for(int i = 0; i<main2dArray.length; i++) {
for(int j=0; j<main2dArray[0].length;j++)
main2dArray[i][j] = arrayL.get(i)[j];
}//for
br.close();
}catch (IOException e) {
System.out.println("error");
}//try-catch
}//create2d

private static void addStartPosition() {
main2dArray[0][1] = "S";
System.out.println(Arrays.deepToString(main2dArray));
}//addStartPosition

}//class

PS: I set your methods as "static" and,consequently, removed the line Simple2dArray array= new Simple2dArray ();, but it is not necessary to do so.


EDIT1: I've tried your new code (you can see from how it is formatted that I have not really touched it), and it works if i add int counter=0; before the try-catch block:


EDIT2: you cannot use line = br.readLine() in the firs while, or the inner cicle will override the value of line. You have to use another condition, according to your plain...
For example:

   boolean flag=true; //condition
while (flag){

switch (counter) {
case (1):
System.out.println ("case 1");
break;
case (2):
System.out.println ("case 2");
break;
case (3):
System.out.println("case 3");
break;
default:
ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
//System.out.println(line);
String[] splitter= line.split(" ");
arrayL.add(splitter);
}//
mainArray= new String[arrayL.size()][arrayL.get(0).length];
for(int i = 0; i<mainArray.length; i++) {
for(int j=0; j<mainArray[0].length;j++)
mainArray[i][j] = arrayL.get(i)[j];
} //for
break;
}//switch
counter++;

//for example, we can interrupt the cycle
flag=false; //You must set your own condition!
}//while

Now your output will be:

screen

...that should be the correct one!

Comparing 2D arrays

The first one does not work because a two-D int array is really an array of arrays (that is, an array of objects). The Arrays.equals() method for an array of objects uses equals() to test whether corresponding elements are equal. Unfortunately for your code, equals() for arrays is the default Object implementation: they are equal() only if they are the identical object. In your case, they are not.

In the second case, when you code Arrays.equals and pass two int values, the compiler can't match it to any signature of the Arrays class.

One way to check equality is to use deepEquals:

boolean check1 = Arrays.deepEquals(a2, a3);

Another way is to iterate the outer array explicitly:

boolean check1 = true;
for (int i = 0; check1 && i < a2.length; ++i) {
check1 = Arrays.equals(a2[i], a3[i]);
}

How to Paste two Two-Dimensional Arrays together?

Let:

  • A be the smaller matrix (arrayone)
  • B be the bigger matrix (arraytwo)
  • C be a matrix equal to B (arrayresult) (int [][] C = B;)
  • x and y the inputed coordinates of the upper left corner of where to paste the matrix A in C

You perform a nested double loop in C matrix, starting from (x,y) and going on for as much as A is big, pasting in C the corresponding values from A at every inner iteration (aka for every cell).

Let the code speak for itself:

if(x + A.length <= B.length && y + A.length <= B.length){   //Check for legal input
for (int i = 0; i < A.length; i++){
for (int j = 0; j < A.length; j++){
C[x+i][y+j]= A[i][j];
}
}
return C

Hope I helped!

How do i compare two double type 2D arrays with eachother?

No need for sorting. Just iterate through the array and locate the maximum value.

double maxVal = peak[0][0];
int maxRow = 0;
int maxCol = 0;
for (int row = 0; row < peak.length; row++) {
for (int col = 0; col < peak[row].length; col++) {
if (peak[row][col] > maxVal) {
maxVal = peak[row][col];
maxRow = row;
maxCol = col;
}
}
}
System.out.printf("The highest value: %f is in cell %d, %d%n",
maxVal,
maxRow,
maxCol);

Sum of 2 different 2D arrays

you can do it more generic

public static int[][] summary(int[][] tab1, int[][] tab2, int x) {
int maxLenX = tab1.length > tab2.length ? tab1.length : tab2.length;
int maxLenY = tab1[0].length > tab2[0].length ? tab1[0].length : tab2[0].length;
int[][] finalTab = new int[maxLenX][maxLenY]; // i took sizes of bigger one
if (x < 0) {
for (int i = 0; i <= finalTab.length - 1; i++) {
for (int j = 0; j <= finalTab[i].length - 1; j++) {
if (i > tab1.length - 1 || j > tab1[i].length - 1) {
finalTab[i][j] = tab2[i][j];
} else if (i > tab2.length - 1 || j > tab2[i].length - 1) {

finalTab[i][j] = tab1[i][j];
} else {
finalTab[i][j] = tab1[i][j] + tab2[i][j];
}
}
}
for (int i = 0; i < finalTab.length; i++) {
for (int j = 0; j < finalTab[i].length; j++) {
System.out.print(" " + finalTab[i][j] + " ");
}
System.out.println();
}
}
return finalTab;
}

so you can call it wether like that

summary(tab2, tab1, -1);

or

summary(tab1, tab2, -1);

How to compare two 2D Arrays in Java?

Your logic looks almost spot-on already. The only issue I see is in the logic handling the case where both arrays are not null and have the same first dimension. You should be returning false if any index does not have matching lengths:

public static boolean arraySameSize(int[][] a, int[][] b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length != b.length) {
return false;
}

// if the code reaches this point, it means that both arrays are not
// null AND both have the same length in the first dimension
for (int i=0; i < a.length; i++) {
if (a[i] == null && b[i] == null) {
continue;
}
if (a[i] == null || b[i] == null) {
return false;
}
if (a[i].length != b[i].length) {
return false;
}
}

return true;
}

Follow the demo link below to see some of examples of this method working correctly.

Demo



Related Topics



Leave a reply



Submit