Jump to content

ArrayList Help

- - - - -

  • Please log in to reply
14 replies to this topic

#1
noel222

noel222

    Newbie

  • Members
  • PipPip
  • 25 posts
nevermind

Edited by noel222, 23 February 2012 - 03:32 PM.


#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

have to write a new class that puts each teammate in an arraylist
Are you going to define a TeamMate class to hold each teammate's goals and other data?

When you get the class, create one for each teammate and add it to the arraylist.
Then to sum up the goals for the all the teammates, write a loop that gets the object for each teammate and gets the goal count from it and adds that to the total.

#3
noel222

noel222

    Newbie

  • Members
  • PipPip
  • 25 posts
I already have a class where I state each teammates goals. Now I want to put each teammate into the array and then have all the goals added together to create the total goals for the team. But, I literally have no clue on how to do this. Can you please elaborate.

Thank You

#4
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
If you have a TeamMate class, then you need to add an instance of it for each teammate to the ArrayList object.
See the API doc for the ArrayList class for the method to use to add objects to the ArrayList.

When you have added all of the teammate objects to the class, you need a loop to get each object from the arraylist, get the goal from that object and add that to the total sum.

#5
noel222

noel222

    Newbie

  • Members
  • PipPip
  • 25 posts
nevermind

Edited by noel222, 23 February 2012 - 03:32 PM.


#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
That is not what I was thinking of.
Create a new class: TeamMate that will hold the name and number of goals.
Create instances for each teammate and add them to the list, etc as I've already said above.

Be sure to define the ArrayList outside of any methods so that all methods can see it.

#7
noel222

noel222

    Newbie

  • Members
  • PipPip
  • 25 posts
I know I keep asking you the same question, but I really am not understanding. I thought that the teammate.add code added each teammate with the values that I have from my teammate class. Then the rest of my code would add up the values of from the teammate class. I am obviously not seeing this correctly, but I am not sure of what to do.

#8
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Take it one small step at a time.
Add the teammates to the array list and print out the contents of the arraylist by printing the teammate variable.
System.out.println(teammate);
Check if all the data is there.

#9
noel222

noel222

    Newbie

  • Members
  • PipPip
  • 25 posts
I attempted to do this, but it did not work. I searched online, and I cannot seem to add each teammate and the value associated with it to the table. In my code, I have "chris" and the other teammates, and I know that is not adding the actual value of chris, but I just cannot seem to figure out how to do this. When I don't use the "", I get an error saying chris can't be resolved to a variable

#10
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
You need to define a class TeamMate that has two values: the name and the number of goals.
Create an instance of this class with the two values and add that to the arraylist.

#11
noel222

noel222

    Newbie

  • Members
  • PipPip
  • 25 posts
Ok, so I have decided to manually enter the number of goals of each teammate instead of calling the value from another class. This is what I have now, but it still is not adding the values together and returning the sum

#12
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

it still is not adding the values together
Please post your code that shows the problem.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users