Page 1 of 1

[HD Pg 0, Sec. 7.9.0 - exercises]

Posted: Tue Apr 21, 2020 3:06 pm
by bankimis
public class Main {

public static void main(String[] args) {
int sumRow = 0;
int sumColumn = 0;
int [][] myInts = {{1,2},{1,2,3},{1,2,3}};
for (int i =0;i<myInts.length;i++) {
for (int j=0;j<myInts.length;j++){
sumRow = sumRow + myInts[j];
}
System.out.println(sumRow);

}
}
}

Re: [HD Pg 0, Sec. 7.9.0 - exercises]

Posted: Wed Apr 22, 2020 3:20 am
by admin
Are you asking whether this is the correct answer?

Re: [HD Pg 0, Sec. 7.9.0 - exercises]

Posted: Mon Aug 02, 2021 2:31 pm
by enthunoob
Typo in question 6:
6. The following code contains a mistake that is quite common while using nested for loops. Identify the problem, fix it and print out all the elements of chars array.

String[][] chars = new String[2][];
chars[0] = new String[2];
chars[1] = new String[4];
char cha = 97;
for (char c = 0; c < chars.length; c ++) {
for (char ch = 0; ch < chars.length; ch++) {
chars[c][ch] = "" + cha;cha++;
}
}

What will happen if char[0] is initialized as new String[1] instead of new String[2]?
Surely chars[o] is meant to be written there.