Quick question about Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wishbone34
    New Member
    • Mar 2007
    • 10

    #1

    Quick question about Arrays

    Hi, say i have a class and I create an array within the constructor in the class, and then i have a method and i want to add to the array in that method, how would I go about doing that? (my array is an array of Grade objects in a Grade support class i have made)

    here is what i have been trying:
    [CODE=java]public class GradeList {

    public GradeList(int n) {
    Grade[] grades= new Grade[n];
    }

    public void add(double max, double weight){
    int i=0;
    grades[i]=new Grade(max, weight);
    //i get an error saying that variable grades cannot be found
    i++;
    }
    }[/CODE]

    Any help that would allow me to do what im trying to accomplish would be appreciated, thank you!
    Last edited by Ganon11; Oct 29 '07, 01:31 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    That's because grades is declared (and thus destroyed) within your constructor. It no longer exists when you get to add. Make grades a private data member, declared outside the constructor and any methods. Then take out the Grade keyword in front of grades in your constructor, and you should be fine.

    Comment

    Working...