Results 1 to 10 of 10
- 06-09-2011, 12:28 AM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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.
Java 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 #2
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 seeingJava Code:int[] sodaSum = new int[SODAS]; int[] personSum = new int[PEOPLE];
which is basically the same except the length is hard coded.Java Code:int[] arr = new int[10];
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?Java Code:sodaSum[soda] += scores[soda][person]; personSum[person] += scores[soda][person];
- 06-09-2011, 01:04 AM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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 #4
No, that is attempting to assign a single int to an int array.
Yes it is creating an array. sodasum is a variable that references a 1D array of ints.instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an 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 arraysodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array?
- 06-09-2011, 01:46 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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
- 06-09-2011, 02:55 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Thank you. Yes I still am reading "Java Software Solutions" after I had to take a long break due to other life things. But now I am back, and I am on page 405 out of about 700. Then I go back to "Head First Java" and finish that. Then I go to "Thinking in Java" and finish that. Then I go to effective java. I am not sure whether to do effective java or thinking in java first, but I will have to take a closer look at effective java. Also, the "Java Software Solutions" program exercises would have too many future-chapter-dependent requirements, so I decided to read the whole book then tackle the chapter program exercises. Because I just got frustrated by the first few chapter exercises when they would require something not covered yet. But it is great to be doing the book again and being on the forum, great people on this forum.
- 06-09-2011, 04:55 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I know how the life interference stuff goes. I've been busy with some stuff lately and haven't been reading as much as I'd like. I'd say read head first java before effective java. I am just finishing effective java and I am really enjoying it. It isn't necessarily an intro book though. It gives you a lot of great things to keep in mind when designing and writing code.
If you are feeling good, you could consider trying effective java and thinking in java at the same time and back off whichever is too challenging.
- 06-09-2011, 04:59 AM #10
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Similar Threads
-
How to plot graph in java with multiple lines for given samples (stored in arrays)?
By theWall in forum Java 2DReplies: 11Last Post: 01-14-2012, 06:46 PM -
Please explain these 2 lines of code to me..
By murphaph in forum New To JavaReplies: 10Last Post: 01-19-2010, 02:11 PM -
question about arrays
By broganm1 in forum New To JavaReplies: 3Last Post: 02-13-2008, 02:29 AM -
Removing empty lines from code using Eclipse
By javaplus in forum EclipseReplies: 1Last Post: 12-14-2007, 09:21 PM -
Unify these 3 lines of code
By ulykidjoe in forum Advanced JavaReplies: 4Last Post: 07-13-2007, 01:15 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks