java two-dimensional array out of extension - java

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
}
}

Related

How can I design the given task in Linked List in Java?

Given a link list of n floating-point numbers, it returns a two-dimensional link list, say M, of size n × n in which the entry
M[i][j] for i ≤ j contains the average of the array entries A[i]
through A[j]. That is: if i ≤ j, then M[i][j] = (A[i] + · · · + A[j])/( j − i + 1) , whereas for i > j we have that M[i][j] = 0.
I have already designed this task in Array but Now I want to do it in Arraylist.
public static void main(String[] args) {
System.out.println("Enter Element");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
float[][] a = new float[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j]= sc.nextInt();
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i > j) {
a[i][j]=0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < j) {
a[i][j] = 0;
for (int k = i; k <= j; k++) {
a[i][j] += a[k][k];
}
a[i][j] /= (j - i + 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(" "+a[i][j]);
}
System.out.println("");
}
}
I have tried with the same idea but I am getting at this stage OutOfBoundException
for (int i = 0; i < lis.length; i++) {
for (int j = 0; j < lis.length; j++) {
if (i > j) {
lis[i][j].add(j, zero);
}
}
}

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)

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++)

Why does my program produce no output while it's compiling fine?

I want to create a two dimensional array. I am able to compile but not able to run
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; k++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
You have an endless loop: for(j=0;j<5;k++), you have to write for(j=0;j<5;j++)
You increment k instead of j
You have an endless loop. You are incrementing k instead of j:
for(j=0;j<5;k++)
You should change it both times to
for(j=0;j<5;j++)
Here... this should work. Just change your sub-loops making it j++ instead of k++ both top and bottom
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
I think you've mixed up the k and j variables in the second for-loop "block". When I alter it to:
...
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
...
I get the following printed to my console:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Is it what you wanted?
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
}
you can resolve this problem

Having Trouble using For Loops to make two triangles of different characters fit into a rectangle?

Examples of input:
3
4
Examples of output (assume that spaces = new lines.)
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
So far, the fragment of code that attempts to print this is (Assume that all variables are pre-defined):
public int getSize()
{
for (int i = size; i > 0; i--){
for (int j = 1; j < size; j++){
out.print("Q");
out.print("H");
}
out.println("");
}
return 0;
}
It just prints: (assume that spaces = new lines.)
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
For input of 5. I'm not quite sure how to make it print only the number of times of its respective integer value. Can someone explain?
You could break the inner loop it two, like this:
for (int i = size; i > 0; i--) {
for (int j = 0; j < i; j++) {
out.print("Q");
}
for (int j = i; j < size + 1; j++) {
out.print("H");
}
out.println();
}
Output:
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
Or if you don't want to break the loop, you can use the ternary operator:
for (int i = size; i > 0; i--) {
for (int j = 0; j < size + 1; j++) {
out.print(j < i ? 'Q' : 'H');
}
out.println();
}
Try this
for (int i = 0; i < size; i++) {
for (int j = 1; j <= size-i; j++) {
System.out.print("Q");
}
for (int k = 0; k <= i; k++) {
System.out.print("H");
}
System.out.println("");
}
try this code block instead:
int j=0;
for (int i = size; i > 0; i--)
{
j=0;
while(j < i)
{
out.print("Q");
j++;
}
j=i;
while(j < size+ 1)
{
out.print("H");
j++;
}
out.println();
}
Tested with sample inputs. Working fine
public int getSize() {
for (int i = 1; i < size+1; i++) {
for (int j = 0; j < size+1; j++) {
int Qtimes = size-i;
if(j <= Qtimes) {
System.out.print("Q");
} else{
System.out.print("H");
}
}
System.out.println("");
}
return 0;
}
This works if the input is 4 - for example -change it to any number
public int getSize()
{
int cnt = 0;
int i,j,k = 0;
for ( i = 4; i > 0; i--){
for ( j = 0; j < i; j++){
System.out.print("Q");
}
cnt ++;
for( k = 0 ; k <cnt ; k++) {
System.out.print("H");
}
System.out.println("");
}
return 0;
}
output is
QQQQH
QQQHH
QQHHH
QHHHH

Categories