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.
Related
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);
}
}
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.
private void enterbtnActionPerformed(java.awt.event.ActionEvent evt) {
int [][] array = new int [4][4]; // my array
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
array[i][j]= ;// use for feeding the code
}
popfield.setText(Arrays.deepToString( array ) );
}
I want to insert integers into 2d array via 2 textfields one for columns and one for rows elements via two text fields xfield and yfield
So.. For create 2D int array:
//int 2 dimensional array
int[][] array = null;
//your fields values
final int xFieldVal = 5;
final int yFieldVal = 7;
//values to fill into array
final int minArrayVal = 50;
final int maxArrayVal = 100;
//create matrix / grid with dimensions (xFieldVal x yFieldVal)
array = new int[xFieldVal][yFieldVal];
(that creates recangle xFieldVal x yFieldVal- or Y*X..)
While you have rectangle array you can acces to all value for filling eg. like that:
//random generator
Random rnd = new Random();
for (int i = 0; i < xFieldVal; i++) {
for (int j = 0; j < yFieldVal; j++) {
//generate new int in interval
array[i][j] = minArrayVal + rnd.nextInt(maxArrayVal- minArrayVal+ 1);
}
}
While you will dont have rectangle array (you can have eg. just 1st dimension fixed, and 2nd not- I mean each "row" can have difference "columns" count), you have to use that loop:
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
//printing next line
System.out.println();
}
Eg. to find min and max value, you can use that:
//set on the max possible (every value should be less than that)
int min=Integer.MAX_VALUE;
//set on the min possible (every value should be more than that)
int max=Integer.MIN_VALUE;
//iteration through 1st index (eg. iteration through rows)
for (int i = 0; i < array.length; i++) {
//iteration through 2nd index of 1st index (eg. through all columns)
for (int j = 0; j < array[i].length; j++) {
//compare and assign if array value is less than actual found min
if(min > array[i][j]){
min = array[i][j];
}
//compare and assign if array value is more than actual found max
if(max < array[i][j]){
max = array[i][j];
}
}
}
I dont think you understand what a 2D array is
think of it as a grid. you insert into a single cell at a time.
a single cell belongs to a row and a column hence has a row number and column number...like an excel work book? cell b5?
so if you want to input numbers all you gotta do is have a single textfield lets call it txt
the rest is as follows
private void enterbtnActionPerformed(java.awt.event.ActionEvent evt)
{
int [][] array = new int [4][4]; // my array
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
array[i][j]= Integer.parseInt(txt.getText());// use for feeding the code
}
popfield.setText(Arrays.deepToString( array ) );
}
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++)
{
....
}
}