• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array to ArrayList

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having trouble adding to ArrayList. I have to convert the normal array to an arraylist.


16.01 Assignment Instructions
Instructions: For this assignment, you are going to use traversal methods.
1. Create a folder called 16.01 Assignment in your module 16 assignments folder.
2. Create a class called Candidate.
a. Candidate will need instance variables name and numVotes (of types String
and int, respectively).
b. Candidate will need appropriate methods and constructors. Make sure to have a
toString() method that prints the name of the Candidate along with the
number of votes.
c. Save the class as Candidate.java.
3. Create a class called TestCandidate and save it as TestCandidate.java.
a. Make sure that you create an array called election. Add the following
Candidates with their votes:
Candidate Vote
John Smith 5000
Mary Miller 4000
Michael Duffy 6000
Tim Robinson 2500
Joe Ashtony 1800
b. Create a method called printVotes() that traverses through the array and
prints out each element.
c. Create a method called getTotal() that traverses through the array and counts
the total of the votes for all Candidates. It should return that number.
d. Create a method called printResults() that traverses through the array and
creates a table with columns for Candidate name, followed by votes received, and
then percentage of total votes (you can round these values). Note that this method
will need to call getTotal() to use in constructing the third column. After
printing the table, print a line showing the total number of votes for all
Candidates.
e. Test your methods. Your output should be similar to that shown below: 4. Now create a class TestCandidate2 and save it as TestCandidate2.java.
a. Create the same items as for TestCandidate; however, use an ArrayList
instead.
b. Output should still look the same as for TestCandidate.



I have done all of this fine. Just the last step is giving me trouble.

Here is my code.





Any help would be great...

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean to convert an array to an ArrayList? Or more precisely add all its elements to a new ArrayList?
I suggest you remember that an ArrayList is a List and a List is a Collection and you can find an ArrayList constructor and a method of the Arrays class. You can do the thing in two stages, but you can write both stages in one line if you so wish. I think I ought not to say any more at the present.

Alternatively you can iterate the array with a loop and add each array element to the ArrayList.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use an enhanced for loop rather than an ordinary for loop to iterate the whole of a List in a read‑only fashion.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you appear not to have to change an array to a List. I think your technique with add(new Candidate... is a good way to do it.

Have you not come across array initialisers? I shall change some of your code to
 
reply
    Bookmark Topic Watch Topic
  • New Topic