calling an array....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShaveDave27
    New Member
    • Apr 2007
    • 22

    calling an array....

    Hi,
    basically i have got the code all sorted just need to know how to call the array - heres the code;

    public int sortArray()
    {
    Comparable[] a = {People.ArrayLis t(people[])};
    Comparable[] sortedArray = main.sort(a);
    }


    the bold section is the part i am stuck with.

    My array is in class People and in method ArrayList.
    My array is called people.

    any ideas would be great,
    thanks
    dave
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Instead of using people[], try people.

    Comment

    • ShaveDave27
      New Member
      • Apr 2007
      • 22

      #3
      Originally posted by Ganon11
      Instead of using people[], try people.
      Hi,
      Thanks for the reply, i tried it as just people and it didnt compile either but it had a different error message, first it said ".class expected". Now it says cant find variable people.

      any ideas?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I'm not sure what you're trying to do...Try

        Code:
        Comparable[] a = people;
        since each Person is a Comparable object, and people is an array of People, this might work.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Ganon11
          I'm not sure what you're trying to do...Try

          Code:
          Comparable[] a = people;
          since each Person is a Comparable object, and people is an array of People, this might work.
          No it doesn't work for a very clear reason but extremely hidden: if a D is a B
          that doesn't make an array of D an array of B. Here's a nice example:
          Code:
          class MeansOfTransportation { ... }
          class Car extends MeansOfTransportation { ... }
          class Submarine extends MeansOfTransportation { ... }
          Now a carpark is a "Car[]", but a carpark isn't a "MeansOfTranspo rtation[]"
          because you don't want any submarines in there ;-)

          kind regards,

          Jos

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            I don't know who you're fooling - I have submarines in my carpark!

            Comment

            Working...