Quantcast

Maximum PC

It is currently Sat Dec 20, 2014 12:57 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Error: casting
PostPosted: Thu Sep 18, 2008 9:04 pm 
Little Foot
Little Foot

Joined: Thu Nov 09, 2006 12:47 pm
Posts: 120
Location: Land of the Lost Newbs
The problem occurs when I try cast "list" as "Person[]" in order to access the compare method from the ComparePerson class.

Code:
package personlist;

import java.util.Arrays;


public class CompareTest
{
    public static void main(String[] args)
    {

        Comparable[ ] list = new Comparable[ 5 ];
       
        for( int p = 0; p < list.length; p++)
        {
           list[ p ] = new Person( (byte)(31 / (p + 1) ), "Ryan" + ( -p ), "002" + p, 185.5 + p * p );
           
           System.out.println( list[p].toString() );           
        }
       
        Arrays.sort( list );
       
        for( Comparable P : list )
        {         
        System.out.println( P.toString() );       
        }
       
        Here>>>Arrays.sort( ( Person[] ) list, new ComparePerson() );
       
        for( Comparable pP : list)
        {           
        System.out.println( pP.toString() );       
        }       

    }

}

Everything runs fine up until this(main method) point, but the funny thing is no error shows up until run time. If I change the call to "list" in any way I get an error pertaining to the sort in this line (Arrays.sort( ( Person[] ) list, new ComparePerson() );, everything else is fine.

Here is my ComparePerson class:
Code:
package personlist;

import java.util.Comparator;

public class ComparePerson implements Comparator<Person>
{
    public int compare( Person p1, Person p2 )
    {   
        if( p1 == p2)
        {
            return (int)p1.getHeight() - (int)p2.getHeight();
        }
       
        else
           
        return p1.getAge() - p2.getAge();
    }

}

Here is my Person class:
Code:
package personlist;

public class Person implements Comparable< Person >
{
    private byte age;
    private String first;
    private String id;
    private double height;

    public double getHeight()
    {
        return height;
    }

    public String getFirst()
    {
        return first;
    }

    public byte getAge()
    {
        return age;
    }

    public String getId()
    {
        return id;
    }

    public Person()
    {
    }

    public Person(byte age, String first, String id, double height)
    {
        this.age = age;
        this.first = first;
        this.id = id;
        this.height = height;
    }

   
    public int compareTo( Person y)
    {
        return first.compareTo(y.first);
    }
       
    public String toString( )
    {
        return String.format( "%-10s%-10d%-10s%-10.2f", first, age, id, height );
    }

}




The objective is to evaluate and sort the data by name, then age, and if age is equal use height as the final sorting method. I can fix this aspect of the code, but if I can't even evaluate the output, I can't fix the mechanics in the secondary evaluation of the data. :cry:


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 4:08 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Arrays aren't Lists. Why do you need to cast? You should be able to (using either a list or an array) access pairs of "persons" to compare.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 4:14 pm 
Little Foot
Little Foot

Joined: Thu Nov 09, 2006 12:47 pm
Posts: 120
Location: Land of the Lost Newbs
The cast was to create a list of type person as an array to evaluate the sorting in the ComparePerson class.

I have realized if I change line 11, where I create "Comparable[ ] list = new Comparable[ 5 ];"
to
Comparable[] list = new Person[5];

it creates a new array of type person which the cast accepts and then evaluates the code in ComparePerson to finish the evaluation.

If you can think of a better way please let me know.

Quote:
Arrays aren't Lists. Why do you need to cast?


I know they are not, but what they do is make lists when you need them to.
The problem stemmed from not creating the new array reference.

Please, any input in this regard would be helpful as I am a bit rusty with technical JAVA jargon.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 8:37 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
Ask your teacher, professor or TA?


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.