Was looking to see if i can get a bit of help with a problem. So i am currently trying to loop through sections of a 2d array and unsure how to do it.
essentially I’m going to have an array 4x4 or 9x9 or 25x25, and i need to be able to iterate over blocks and check for duplicates.
for example 4x4, i would iterate over 4 2x2 arrays.
the 9x9 would be 9 3x3 arrays etc.
been trying for a while but have had no luck
i have tried this
any help would be great,
cheers
If the arrays are always 2d then you could do something like this:
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
// Initialize the 2d array
int size = 3;
int[][] table = new int[size][size];
for (int row = 0; row < size; row ++)
for (int col = 0; col < size; col++)
table[row][col] = (int) (20.0 * Math.random());
// Scan for duplicates
List seen = new ArrayList();
List duplicates = new ArrayList();
for (int row = 0; row < size; row ++) {
for (int col = 0; col < size; col++) {
boolean exists = seen.contains(table[row][col]);
if (!exists) {
seen.add(table[row][col]);
continue;
}
duplicates.add(table[row][col]);
}
}
System.out.println(seen);
System.out.println (duplicates);
}
}
Related
I need help completing one task from Java book that I read. I need to create a 3-dimentional array of int that will be able to store 30 values.
It's described as cuboid containing cubes. Each cube is supposed to be a cell and they should store ints from 30 to 59. How should it look like? I try to draw it but it's pretty hard for me. Here is what I've tried.
public class cw124{
public static void main (String[]args){
int tab[][][]=new int[31][30][30];
int wypelniacz=30;
for (int i=0; i<tab.length; i++){
for (int j=0; j<tab[j].length; j++){
wypelniacz=30;
for (int k=0; k<tab[k].length; k++){
tab[i][j][k]=wypelniacz++;
}
}
}
for (int i=0; i<tab.length; i++) {
for (int j=0; j<tab[j].length; j++){
for (int k=0; k<tab[k].length; k++){
wypelniacz=30;
tab[i][j][k]=wypelniacz++;
System.out.println("Row "+i+" Cell 1 "+j+" Cell 2 "+k+" "+tab[i][j][k]);
}
}
}
}
}
Your 3D array currently has 31*30*30 = 27,900 cells. If you need a 3D array with 30 cells, you can do this:
int tab[][][]=new int[5][3][2];
This will give you a 3D array with 5*3*2 = 30 cells.
You can imagine each value in square brackets to be the length of one side of the cuboid.
The next step would be:
int counter = 30;
for(int i = 0; i < tab.length; i++)
{
for(int j = 0; j < tab[0].length; j++)
{
for(int k = 0; k < tab[0][0].length; k++)
{
tab[i][j][k] = counter;
counter++;
}
}
}
This will populate all the cells with numbers from 30 to 59.
I think the following code may help you understand the task.
You have to think of the 3 dimensions as a cube, the cube contains grids, each grid has rows which then have multiple columns)
(imagine a Rubik's cube, which has 3 layers=grids, each grid then has 3 rows and each of those rows again has 3 columns)
final int gridCount = 5;
final int rowCount = 5;
final int colsPerRow = 15;
final int[][][] cube = new int[gridCount][rowCount][colsPerRow];
for (final int[][] grid : cube) {
for (int col = 0; col < grid.length; col++) { //just to show the two different versions of 'for'
final int[] row = grid[col];
row[col] = 42+ col; //set it to whatever number
}
}
I need to delete row and column where max value in 2d array exist,please suggest how to do it.
Here is an array in which specific row and column must be delted. I suppose here can be used aaraycopy
import java.util.Scanner;
public class Test{
public static void main (String[] args){
int maxValue=0;
int[][] multiplyTab = new int[5][10];
int row = 0;
int column=0;
for (int i = 0; i < multiplyTab.length; i++) {
for (int j = 0; j <multiplyTab[i].length ; j++) {
multiplyTab[i][j] =((i+1)*(j+1));
System.out.print(multiplyTab[i][j] + "\t");
}
System.out.println();
}
for (int i = 0; i < multiplyTab.length; i++) {
for (int j = 0; j < multiplyTab[i].length; j++) {
if (multiplyTab[i][j] > maxValue) {
maxValue = multiplyTab[i][j];
row=i;
column=j;
}
}
}
}
}
To delete row i from a 2D array like yours, first create a new 2D array that is one element shorter (4 instead of 5 in your example). Copy rows 0 through i - 1 and rows i + 1 through originalTable.length - 1 into the new array.
Edit: when maxValue is in the last row (as here if I am not mistaken) and hence the last row is to be deleted, it is of course a bit easier since there are no rows i + 1 through originalTable.length - 1 to copy.
To delete column j do similarly to every inner array of the table.
I'm trying to populate a 2d array in java for a sudoku board. The numbers come from a csv file. The issue is the code just reads the first four numbers, then restarts at 0 again for a new row. How do I stop this from happening, and get it to continue to the end of the numbers?
String[] lines = Cell.toCSV().split(",");
int[] intArray = new int[lines.length];
for (int i = 0; i < intArray.length; i++) {
intArray[i] = Integer.parseInt(lines[i]);
} //convert string to int
int[][] dataArray = new int[4][4]; //4x4 sudoku game
for (int col = 0; col < size; col++) {
for (int row = 0; row < dataArray[col].length; row++) {
dataArray[col][row] = intArray[row];
}
You need a separate counter for the original array :
int index = 0;
for (int col = 0; col < dataArray.length; col++) {
for (int row = 0; row < dataArray[col].length; row++) {
dataArray[col][row] = intArray[index++];
}
This is assuming the intArray has enough values to populate the 2D array. You should probably validate that prior to this loop.
BTW, the first dimension of a 2D array is usually considered as the row, not the column, so your loop variable names are a bit confusing.
i am trying to calculate the sum of elements after the diagonal in 2D array but the problem is that the sum is always equal ZERO it do not change the answer.
where is the mistake in my code and how to fix it ?
i tried to use nested for loop and o tried to use one for loop but in both cases th eanswer still 0.
this is my code:
package test8;
import java.util.Scanner;
public class Question2 {
private int row = 4;
private int col = 4;
private int[][] matrix;
public Question2(int trow, int tcol) {
this.row = trow;
this.col = tcol;
}
public Question2(int trow, int tcol, int[][] m) {
this.row = trow;
this.col = tcol;
this.matrix = m;
}
public int[][] fill() {
int[][] data = new int[row][col];
Scanner in = new Scanner(System.in);
for (int row = 0; row < data.length; row++) {
for (int col = 0; col < data[row].length; col++) {
System.out.println("enter the elementss for the Matrix");
data[row][col] = in.nextInt();
}
System.out.println();
}
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < data[row].length; col++) {
System.out.print(data[row][col] + " ");
}
System.out.println();
}
return data;
}
public int calculate(int[][] num) {
int sum = 0;
for (int row = 0; row < num.length; row++) {
//for (int col = row + 1; col < num[row].length; col++) {
// if(row == col){
System.out.println(row);
sum += num[row][row];
// }
//}
}
System.out.println("the sum is: " + sum);
return sum;
}
public static void main(String[] args) {
Question2 q2 = new Question2(3, 3);
int[][] ma = q2.fill();
q2.calculate(ma);
}
}
this is the output:
1 2 3
4 5 6
7 8 9
0
1
2
the sum is: 15
Problem is that in your fill method you are creating local data array, which is filled with values and returned as result, but in your main method you are not storing this array anywhere. Instead in calculate you are using int[][] ma = new int[3][3]; which is filled with 0.
So maybe change your code to something like
Question2 q2 = new Question2(3, 3);// you don't even need to pass array here if you
// plan to generate new one, so consider removing
// array from argument list in this constructor
int[][] ma = q2.fill();//store array with elements in `ma` reference
q2.calculate(ma); //now `ma` has elements so we can do some calculations
Anyway your code seems strange. It looks like you want your class to store some array, but you are not using this array anywhere (except in fill method where you are using its length properties to fill data array - which seems wrong since data can have different size than matrix).
Maybe in fill method you shouldn't be creating new data array, but instead you should fill matrix array? Also in that case you don't need to pass this array as argument in constructor, but just create new array based on row and col values.
This way you will not even need calculate method to get any array from outside, just use matrix field.
In other words your code can look more like
// remove entirely this constructor, you will not need it
public Question2(int trow, int tcol, int[][] m) {
this.row = trow;
this.col = tcol;
this.matrix = m;
}
// but use only this one
public Question2(int trow, int tcol) {
this.row = trow;
this.col = tcol;
this.matrix = new int[trow,tcol];// just make sure it will initialize `matrix`
}
Now you can simply use new Question2(3,3) and be sure that it will also have empty array with correct sizes.
Time for fill method. Don't create local int[][] data array, just use matrix instead, so change
data[row][col] = in.nextInt();
to
matrix[row][col] = in.nextInt();
Last thing is calculate method. You don't need it to actually take any array from user, because you already have matrix array inside your class so you can simply reuse it. So instead of calculate(int[][] num) make it calculate() and instead of num use matrix.
So now your code can look like
Question2 q2 = new Question2(3, 3);
q2.fill();
q2.calculate();
You have multiple structure problem but, responding your question, replace this line:
q2.fill();
by:
ma = q2.fill();
I have 2 1d arrays and im trying to populate them into a single 2d array in JAVA.
For instance:
x[] = {2,5,7,9}
y[] = {11,22,33,44}
The results should then be:
result[][] = {{2,5,7,9}, {11,22,33,44}}
How do I go about this? I currently have something like this:
for(int row = 0; row < 2; row++) {
for(int col = 0; col == y.length; col++) {
???
}
}
Im sort of stuck from there...
2D array is an array of arrays. So why don't you try this?
int result[][] = {x,y};
And to make sure that it is so simple and works, test this:
for(int i=0; i<result.length; i++)
{
for(int j=0; j<result[0].length; j++)
System.out.print(result[i][j]+ " ");
System.out.println();
}
Try this:
ArrayList<Integer[]> tempList = new ArrayList<Integer[]>();
tempList.add(x);
tempList.add(y);
Integer result[][] = new Integer[tempList.size()][];
result = tempList.toArray(tempList);
You have to revert the row and column indices
for(int row = 0; row < 2; row++)
{
for(int col = 0; col = y.length; col++)
{
....
}
}