Question about some lines of code arrays

• 06-09-2011, 12:28 AM
Question about some lines of code arrays
Hi, I have a question about some code in this program, it deals with arrays. I have commented the questions on the lines.
Any help greatly appreciated. Thank you.

Code:

```//******************************************************************** //  SodaSurvey.java      Author: Lewis/Loftus // //  Demonstrates the use of a two-dimensional array. //******************************************************************** import java.text.DecimalFormat; public class SodaSurvey {   //-----------------------------------------------------------------   //  Determines and prints the average of each row (soda) and each   //  column (respondent) of the survey scores.   //-----------------------------------------------------------------   public static void main (String[] args)   {       int[][] scores = { {3, 4, 5, 2, 1, 4, 3, 2, 4, 4},                         {2, 4, 3, 4, 3, 3, 2, 1, 2, 2},                         {3, 5, 4, 5, 5, 3, 2, 5, 5, 5},                         {1, 1, 1, 3, 1, 2, 1, 3, 2, 4} };       final int SODAS = scores.length;       final int PEOPLE = scores[0].length;       int[] sodaSum = new int[SODAS];[COLOR="green"] //I don't understand this line[/COLOR]       int[] personSum = new int[PEOPLE];[COLOR="green"]//I don't understand this line either[/COLOR]       for (int soda=0; soda < SODAS; soda++)         for (int person=0; person < PEOPLE; person++)         {             sodaSum[soda] += scores[soda][person];[COLOR="green"]//I don't understand this line[/COLOR]             personSum[person] += scores[soda][person];[COLOR="green"]//Dont understand this line either[/COLOR]         }       DecimalFormat fmt = new DecimalFormat ("0.#");       System.out.println ("Averages:\n");       for (int soda=0; soda < SODAS; soda++)         System.out.println ("Soda #" + (soda+1) + ": " +                     fmt.format ((float)sodaSum[soda]/PEOPLE));       System.out.println ();       for (int person =0; person < PEOPLE; person++)         System.out.println ("Person #" + (person+1) + ": " +                     fmt.format ((float)personSum[person]/SODAS));   } }```
• 06-09-2011, 12:39 AM
Junky
Code:

```int[] sodaSum = new int[SODAS]; int[] personSum = new int[PEOPLE];```
What don't you understand? Both lines declare and initialise an int array with a length of whatever value is stored in the variables: SODAS and PEOPLE. Perhaps you are more used to seeing
Code:

`int[] arr = new int[10];`
which is basically the same except the length is hard coded.
Code:

```sodaSum[soda] += scores[soda][person]; personSum[person] += scores[soda][person];```
Same for these lines. Rather having a hardcoded number for the index of the array it is using a variable. Or are you not understanding the 2D array part?
• 06-09-2011, 01:04 AM
Sorry I didnt specify what I didn't understand,It just looked like greek. But now I understand more of what I don't understand lol. I thought the above code should be int [] sodasum = SODAS; instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an array. and sodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array? Sorry for the confusion, thank you for helping.
• 06-09-2011, 01:22 AM
Junky
Quote:

I thought the above code should be int [] sodasum = SODAS;

No, that is attempting to assign a single int to an int array.
Quote:

instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an array.
Yes it is creating an array. sodasum is a variable that references a 1D array of ints.
Quote:

sodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array?
It isn't. It is accessing a single value in the 2D array at location [soda][person] and assigning it to location [soda] in the 1D array
• 06-09-2011, 01:46 AM
sunde887
To help clarify, the 2 d array also contains ints. It is simply an array of arrays where each array is filled with ints, so if you index into the first dimension you are looking at an array of ints. If you index into the first and second you are looking at an int. So it's safe to add the item at index [x][y] to a element of the one d array.

I hope this clarifies it somewhat, if not, let me know and I will try to do a better job explaining.
• 06-09-2011, 02:27 AM
Great thank you Junky and Sunde887. I now understand it after writing out the loops and putting in the values like you would a math problem. That is the best way I think to figure out code. Treat it as a math problem. It is nice to see you Sunde887, I was away for a while. But now I'm learning java again every day or I try to. I am starting to see the art of computer programming, especially in these "elegant" little for loop applications that I have to stretch my non programmer brain to understand. The more compact the code, the more ingenious it is, and kind of an art form. Like looking at someone take a huge engine (bad programmer or non programmer) and redoing it the size of a lunch box (good programmer or experienced). I hope I can write an app that good as the one I just showed you. Since I don't have the mindset for it yet. The previous code I showed you is very ingenius to me. Thanks both again. Another hurdle jumped. Derek:o:
• 06-09-2011, 02:55 AM
sunde887
Glad to see you again and hope all is well. How's the book coming? How far in have you gotten? The more you read and practice, the easier things get.
• 06-09-2011, 04:44 AM