3 Replies Latest reply: May 8, 2012 2:40 PM by 935821 RSS

    Problem with lists.

    935821
      Hi,
      I have 2 classes in my program.
      1 class named people and it has 2 variables : int age and String name.
      The second class is PeopleArray that contain list of people class :
      This is what i have at the PeopleArray class :
      ArrayList PArray = new ArrayList();
      People p = new People();
      PArray.add(p);
      Now i want to access the name of one people :
      String name = p.name;
      But i cant do it, how can i do it by the list? (i dont want to use arrays because i dont know what will be the array size).
      I`ll be happy if someone can help me.
      Thanks,
      Or.
        • 1. Re: Problem with lists.
          gimbal2
          932818 wrote:
          Hi,
          I have 2 classes in my program.
          1 class named people and it has 2 variables : int age and String name.
          The second class is PeopleArray that contain list of people class :
          This is what i have at the PeopleArray class :
          ArrayList PArray = new ArrayList();
          People p = new People();
          PArray.add(p);
          Now i want to access the name of one people :
          String name = p.name;
          But i cant do it, how can i do it by the list? (i dont want to use arrays because i dont know what will be the array size).
          I`ll be happy if someone can help me.
          Thanks,
          Or.
          People p = pArray.get(0);
          String name = p.getName();
          What you put in, you have to take out first again if you want to use it. and use \
           tags when posting code. Also, I would stick to the "new to java" forum when you're new to Java.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
          • 2. Re: Problem with lists.
            796440
            932818 wrote:
            Now i want to access the name of one people :
            String name = p.name;
            But i cant do it, how can i do it by the list?
            You'd have to iterate over the list, checking the name of each People object (by the way, Person would be a better name for that class, since one object represents one Person).

            However, if you're frequently going to be looking up a People object by its name field, then you should use a Map that has the name as the key and the corresponding People object as the value. Of course, this assumes that names are unique.
            • 3. Re: Problem with lists.
              935821
              Thanks !
              You helped me alot.