Results 1 to 5 of 5
- 03-03-2011, 11:07 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
[SOLVED]Adding elements before and after array
Hi all,
I'm new to Java and I'm having this problem with adding elements alternately in front and behind an array.
The methods to add the elements I have already figured out.
I also came up with a method to add the elements alternately to the array, but my instructor told me it could be done with a boolean instead of what I have now.
My method now is to keep up count when the data that needs to be added is read and whenever the count odd it adds data BEHIND and whenever it's even it adds data IN FRONT of the array.
Could anyone please tell me how to fix this problem with the boolean my instructor is talking about?Java Code:in.useDelimiter("="); int count = 0; while (in.hasNext()) { String array = in.next(); Scanner arrayScanner = new Scanner (array); if (count%2==0) { coordinateArrayInput.addInFrontArray(readArray(arrayScanner)); } else { coordinateArrayInput.addBehindArray(readArray(arrayScanner)); } count+=1;
Thank you.
Yours,
liQuoriceLast edited by liQuorice; 03-04-2011 at 12:53 AM. Reason: Solved
- 03-03-2011, 11:26 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Suppose you have a boolean flag; it can be either true or false; if it is true you add a value, say, to the front of the array, otherwise you append it to the end. You 'toggle' the value of that flag like this:
You have to do that as the last statement of the body of your loop and you have to initialize its value to true or false, depending on if you want to insert or append your first element.Java Code:flag= !flag;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-03-2011, 08:36 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Dear JosAH,
Thank you for your reply.
Unfortunately it was not the answer I was looking I for (I think...?).
My instructor told me you can do it with a boolean AND without the count to check if it's even or odd.
Still a mystery to me...anyone care to share their knowledge :)?
Thank you.
Yours,
liQuorice
- 03-03-2011, 08:55 PM #4
i think you need to look at Jos' answer again. Unless I'm misunderstanding you're question, Jos' answer should do it.
Basically, he is suggesting this:
This uses a boolean and doesnt use a counter.Java Code:boolean flag = true; while (in.hasNext()) { if (flag) { coordinateArrayInput.addInFrontArray(readArray(arrayScanner)); } else { coordinateArrayInput.addBehindArray(readArray(arrayScanner)); } flag = !flag; }
Hope this explains it better!
- 03-04-2011, 12:52 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Adding elements to an ArrayList
By ArcherSam in forum Advanced JavaReplies: 7Last Post: 01-28-2011, 03:05 PM -
Problem in adding arrayList elements
By cool in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:27 PM -
Adding the Elements of a 2D Array
By RMcLuckie45 in forum New To JavaReplies: 0Last Post: 11-07-2010, 11:04 PM -
Adding elements in array list and vectors using threads
By hina.yousuf@seecs.edu.pk in forum Advanced JavaReplies: 2Last Post: 10-10-2010, 03:07 AM -
Adding elements to an Object Array
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 09-07-2008, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks