Results 1 to 7 of 7

Thread: ArrayList or Object assignment?

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    ArrayList or Object assignment?

    I have class edge:
    Code:
    public class GraphMap
    {
        private ArrayList _vertices;
    
    // ... deleted most of class, only important stuff...
    
        public void addEdge( int start, int end, double dist )
        {
            Vertex tempVert = new Vertex();
            tempVert = _vertices.get( start );
        }
    This gives me the compiler error:
    Incompatible type for =. Can't convert Object to Vertex.
    tempVert = _vertices.get( start );

    Why is this happening and can I get around it? Does using ArrayLists limit me to added classes that inherit Object class? Can I write an assignment operator for my Vertex class, if so what would a sample declaration line look like?


    NOMAD

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Everything in Java inherits from Object by default. To make your code work, you simply need to cast the Object to your object type.
    Code:
        public void addEdge( int start, int end, double dist )
        {
            Vertex tempVert = (Vertex)_vertices.get( start );
        }
    That should work.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    Thanks worked like a charm.
    Another question: I made a few classes that use each other.
    Like a node class and a tree class. They are in different files, how can I #include them in my java program?
    I have package BSTree written at the top of all of them. Is that correct?
    (If you noticed my #include I'm native more to C++/C, how is the include macro done in Java?

    Thanks!
    NOMAD

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    The import keyword is what you're looking for. At the top of your code.
    Code:
    import java.util.*;
    Imports the entire jave.util package.
    Code:
    import java.util.Hashtable;
    imports just the Hashtable class.

    All import does is save you typing. Without using import, you would have to declare a Hashtable like this :
    Code:
    java.util.Hashtable ht = new java.util.Hashtable();
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    so if I imported a class that I made it would be
    import Tree
    ?

    NoMaD

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    If the class that you wish in import is in the current directory then no import statement should be needed. But if the class is defined in a directory other then what is defined in the classpath environment variable then i am pretty sure that an import statement would be needed. I never did get packages and imports to work right so i might be wrong on this.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not entirely correct. You don't need an import if the class you're using is in the same package. You always need an import if it's in a different package.

    And the files must always be in the CLASSPATH or in the same directory, independent from any imports.

    Look at the java docs to find out how the JRE resolves class file locations.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width