Java weird out of bounds? - java

I have no idea why I'm getting out of bounds for this.
int[][] board = new int[3][3];
for (int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; i ++) {
board[i][j] = 0;
}
}

You are incrementing i in the j for loop. Change
for(int j = 0; j < 3; i ++){
to
for(int j = 0; j < 3; j++){

Related

Is this a correct way to analyze a graph? Warshall algorithm

Here's an implementation of Warshall algorithm copied from this site
https://rosettacode.org/wiki/Floyd-Warshall_algorithm#Java
What I need is to analyze this algorithm.I already did but I want to make sure everything is correct.
for (int i = 0; i < weights.length; i++) { //O(E)?
dist[weights[i][0] - 1][weights[i][1] - 1] = weights[i][2];}
for (int i = 0; i < next.length; i++) {//O(v^2)
for (int j = 0; j < next.length; j++)
if (i != j)
next[i][j] = j + 1;
}
for (int k = 0; k < numVertices; k++)//O(v^3)
for (int i = 0; i < numVertices; i++)
for (int j = 0; j < numVertices; j++)
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
next[i][j] = next[i][k];
}
//Finally takes O(v^3)

Need to change all values in a specific row and specific column that intersect to zero

The array is intialized with random numbers ranging between 0-20.
And if any value in that array is 0, this method should change all the values in that row and column to zero.
Instead it wipes out the array and changes all values to zero.
Any thoughts?
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (matrix[row][col] == 0){
for (int k = 0; k < size; k++)
matrix[k][col] = 0;
for (int l = 0; l < size; l++)
matrix[row][l] = 0;
}
}
}
System.out.println("Result:");
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
System.out.printf("[%1$2s]", copy[i][j]);
System.out.println();
}
For anyone who still cares, here is the full code
Random random = new Random();
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
matrix[i][j] = random.nextInt(20);
}
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (matrix[row][col] == 0){
for (int k = 0; k < size; k++)
matrix[k][col] = 0;
for (int l = 0; l < size; l++)
matrix[row][l] = 0;
}
}
}
System.out.println("Result:");
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
System.out.printf("[%1$2s]", matrix[i][j]);
System.out.println();
}

constructing a skew symmetric matrix

I am trying to construct a skew symmetric matrix. I can print the negatives but I am unable to print the diagonal to be 0. Where am I incorrect logically?
public void SkewSymmetric() {
for (int i = 0; i < matrix.length; ++i) {
for (int j = 1; j < matrix.length; i++) {
matrix[i][j] = random.nextInt(BOUND);
}
}
for (int i = 9; i < matrix.length; ++j) {
for (int j = 0; j < i; ++j) {
matrix[j][j] = -matrix[i][i];
}
}
}
Your inner loop in the second block needs to run from i + 1.
Otherwise the trace will be, in general, non-zero.
You are setting all the values in the matrix to 0 and then trying over-write the non-diagonal values.
Instead set only the primary diagonal values to 0.
As pointed out by Jean, your loop values are incorrect.
For the first row random numbers are generated from column 1 to n
For the second row random numbers are generated from column 2 to n and so on.
Try this,
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix.length; j++){
if(i==j){
matrix[i][j] = 0;
}
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = i+1; j < matrix.length; j++) {
matrix[i][j] = random.nextInt();
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < i; j++) {
matrix[i][j] = -matrix[j][i];
}
}
The problem comes from for (int j = i; j < matrix.length; j++): when j == i at the first iteration, it erases the 0 that have been previsouly set.
Therefore you need to change it to for (int j = i+1; j < matrix.length; j++)

Multiplying Muti-Dimensional Arrays

Been having a bit of trouble finishing this program involving two 2D arrays and multiplying them together. Now I was able to construct these arrays with set lengths and then using a number generator to create each array. As for a third array I was able to establish the length of the array, but when placing the three arrays into a method I am still having out of bounds issues.
public class arrayTest1{
public static void main ( String [] args){
int matrix1[][] = new int [5][2];
for (int i = 0; i < matrix1.length; i++)
for (int j = 0; j < matrix1[i].length; j++)
matrix1[i][j] = (int)(Math.random() * 1000);
System.out.println("The array 1 is: ");
for (int i = 0; i < matrix1.length; i++){
for (int j = 0; j < matrix1[i].length; j++){
System.out.print(matrix1[i][j]+" ");
}
System.out.println();
}
int matrix2[][] = new int [2][5];
for (int i = 0; i < matrix2.length; i++)
for (int j = 0; j < matrix2[i].length; j++)
matrix2[i][j] = (int)(Math.random() * 1000);
System.out.println("The array 2 is: ");
for (int i = 0; i < matrix2.length; i++){
for (int j = 0; j < matrix2[i].length; j++){
System.out.print(matrix2[i][j]+" ");
}
System.out.println();
}
int matrixSum[][] = new int [matrix1.length][matrix2[0].length];
matrixMulti(matrix1,matrix2,matrixSum);
System.out.println("The array mutliplied is: ");
for (int i = 0; i < matrixSum.length; i++){
for (int j = 0; j < matrixSum[i].length; j++){
System.out.print(matrixSum[i][j]+" ");
}
System.out.println();
}
}
public static void matrixMutli(int [][] m1, int [][] m2,int [][] totalMatrix){
for(int i = 0; i < m1.length; i++)
for(int j = 0; j < m2[0].length; j++)
for(int k = 0; k < totalMatrix.length; k++)
totalMatrix [i][j] += m1[i][k] * m2[k][j];
}
}
The ArrayIndexOutOfBounds is caused by the fact that your k variable goes from 0 to 5. It needs to go from 0 to 2. The problem is because of the line :
for(int k = 0; k < totalMatrix.length; k++)
The matrix totalMatrix is initialized with int matrixSum[][] = new int [matrix1.length][matrix2[0].length]; and matrix1.length is 5. So totalMatrix.length is 5.
To correct this you need to make sure that k is bounded by either matrix1[i].length or by matrix2.length. These two values need to be the same and you can choose either to be the bound for k.
So this is the code:
for(int i = 0; i < m1.length; i++)
for(int j = 0; j < m2[0].length; j++)
for(int k = 0; k < m2.length; k++)
totalMatrix [i][j] += m1[i][k] * m2[k][j];
Also consider adding code that checks whether m1[i].length == m2.length and throws an IllegalArgumentException if not. If you are still in trouble have a look at matrix multiplication.

java two-dimensional array out of extension

i have this simple code:
double[][] params = new double[][]{{197.0,258.0,427.0,426.0,507.0,524.0,386.0},
{345.0,473.0,484.0,422.0,406.0,291.0,289.0}};
for (int i = 0; i <= params.length; i++) {
for (int j = 0; j <= params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); //line 29
}
}
and the error:
> Caused by: java.lang.ArrayIndexOutOfBoundsException: 7 at
> com.eyecom.gen.generate_xml(gen.java:29)
Why do I get this exception?
In Java, arrays are zero based. Here you're exceeding the array bounds. Use
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
Try
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); // line 29
}
}
indexes in array are from 0 to length-1 so you don't want to use index equal to length.
Change the for loop construct
for (int i = 0; i <= params.length; i++) {
for (int j = 0; j <= params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); //line 29
}
}
to
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
System.out.println("Coordinates: " + params[i][j]); //line 29
}
}

Categories