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.
Related
New to java and am trying to create a program/method that will take an int array, and return another int array but it replaces the values of the indexes with the value of the elements. (Example {2,1,3} will return {0,0,1,2,2,2}
public static void main(String[] args) {
int[] pracArray = {2, 1, 3};
int sum = 0;
for (int i = 0; i < pracArray.length; i++)
{
sum = sum + pracArray[i];
}
System.out.println("Amount of array indexes: " + sum);
int[] newArray = new int[sum];
System.out.println(Arrays.toString(pracArray));
for (int i = 0; i < pracArray.length; i++)
{
for (int j = 0; j < pracArray[i]; j++)
{
newArray[j] = i;
}
}
System.out.println(Arrays.toString(newArray));
}
}
Currently I am getting [2,2,2,0,0,0]. I have tried changing the how many times each for loop iterates with no avail. I have also tried to make the elements of newArray equal to a counter ( int count = 0; and having count++ in the for loop) since the values of the new array will always be 0 - however many runs.
Given the length of your array is 3, your outer 'i' loop is iterating through the values 0,1,2. That means your inner 'j' loop never writes to index 3,4,5 (hence why they are 0 in the output), and why the first 3 indexes are set to '2' (2 is the last indexed processed in the 'i' loop). Try this instead...
int h = 0;
for (int i = 0; i < pracArray.length; i++)
{
for (int j = 0; j < pracArray[i]; j++)
{
newArray[h] = i;
h++;
}
}
Noob to programming... I need to create a function that receives a 2d array and requests user input to fill both the rows and the columns. The error that shows me is "empty statement" / "not a statement" on the last line.
public static void fillMatrix(int [][] pmatrix) throws IOException {
int [][] matrix = new int [pmatrix.length][pmatrix.length];
int i, k; //loop variables
int rows, columns;
for(i = 0; i < pmatrix.length; i++){
print.println("set the value of the row " + (i + 1));
rows = Integer.parseInt(read.readLine());
}
for(k = 0; k < pmatrix.length; k++){
print.println("set the value of the column " + (k + 1));
colums = Integer.parseInt(read.readLine());
}
matrix = {{rows}, {columns}};
}
First, what you do here is reassigning the variables rows and columns in each iteration. So at the end, you will have one single value per row in your matrix.
Second, you're reassigning the local variable matrix to be a Matrix, that has two rows and one column. And since it doesn't have anything to do with the parameter pmatrix, nothing will happen to it after the method returns.
I assume you want to call that method on an empty 2D-Array and fill it with values from the console. To iterate through a 2D-Array, you will need a nested for-loop and access each index in your matrix individually:
public static void fillMatrix(int [][] pmatrix) throws IOException {
for(i = 0; i < pmatrix.length; i++){
for(int j = 0; j < pmatrix[i].length; i++ {
print.println("set the value of row " + (i + 1) + " in column " + (j + 1));
pmatrix[i][j] = Integer.parseInt(read.readLine());
}
}
}
It is easier to understand in the full context.
The following code has a main method that creates a matrix of size 3x3, you can change this if you like.
Next, it calls the fillMatrix method and then the printMatrix method.
fillMatrix goes through each row and for each row, it goes through each column. For an entry in the matrix, it reads an integer using Scanner instead of BufferedReader because it is easier to use.
printMatrix runs through all the entries and prints them as a table.
Running the program and giving 1 2 3 4 5 6 7 8 9 prints
1 2 3
4 5 6
7 8 9
The program
import java.io.IOException;
import java.util.Scanner;
public class Helper {
public static void main(final String[] args) throws IOException {
final int[][] matrix = new int[3][3];
fillMatrix(matrix);
printMatrix(matrix);
}
public static void fillMatrix(final int[][] matrix) throws IOException {
final Scanner scanner = new Scanner(System.in);
for (int i = 0; i < matrix.length; i++) {
final int[] row = matrix[i];
for (int j = 0; j < row.length; j++) {
final int userInput = scanner.nextInt();
row[j] = userInput;
}
}
}
private static void printMatrix(final int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
final int[] row = matrix[i];
for (int j = 0; j < row.length; j++) {
System.out.print(row[j] + " ");
}
System.out.println();
}
}
}
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.
The question is:
Create a method display2DArray().
a) Inside the method, declare a 2D array that will hold the following integers:
{10,20} {11,21}
{15,25} {17,28}.
b) Display this information using two for loops.
public static void display2DArray()
{
int[][] arrays = new int[][]
{
{10, 20}, {11,21}, {15,25}, {17,28}
};
for(int i = 0; i < 3; i++)
{
for(int j = 0; i < 1; j++)
{
System.out.println(arrays[i][j]);
}
}
}
This is what ive come up with, but its not correct.
Can someone tell me what i need to be doing?
You are almost there!
Few things:
1) Typo - In your inner for loop, you are using an "j" instead of "i".
2) The same "j" must be j<=1 OR j<2 because you have 2 columns i.e. 2 elements in each sub-array. So the indexes will be 0 and 1.
3) In your outer for loop, you are using i<3. Since you have 4 rows i.e. 4 sub arrays, your indexes will be 0,1,2,3. So you need to use i<=3 OR i<4.
4) You can print an empty line in the outer for-loop for a better display.
for(int i = 0; i <= 3; i++) // Since you have 4 rows, indexes would be 0,1,2,3
{
for(int j = 0; j <= 1; j++) // Since you have 2 columns, indexes would be 0,1
{
System.out.print(arrays[i][j]+","); // Print each row i.e. sub-array
}
System.out.println(""); // Print an empty line after each row
}
This gives you the output:
10,20,
11,21,
15,25,
17,28,
In your second for loop, you have "i < 1" instead of "j < 1"
Use j in second array
public static void display2DArray()
{
int[][] arrays = new int[][]
{
{10, 20}, {11,21}, {15,25}, {17,28}
};
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 2; j++)
{
System.out.println(arrays[i][j]);
}
}
}
Also the boundaries were wrong
Your Problem with the inner loop the you used . You used i instead of J. So, Your loop will not give you the result as you Expected.
for(int i = 0; i < 3; i++)
{
for(int j = 0; j <= 1; j++) //use j instead of i here.
{
System.out.println(arrays[i][j]);
}
}
Thanks
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j <= 1; j++)
{
System.out.println(arrays[i][j]);
}
}
1.) second for loop condition check is j< 1 not i< 1
2.) first for loop condition check is i<=3
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 ) );
}