Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ArrayList.get() Returning "Incompatible Type"

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default ArrayList.get() Returning "Incompatible Type"

    I'm trying to create a basic class that stores an array of Objects and returns them. I do not have a way of knowing how many objects I will need to store, so I am using an ArrayList instead of a regular Array. However, code that would work with a regular Array does not appear to function with an ArrayList's get() method. Instead, I get an "Incompatible Types" error when I try to return the results of .get().

    Even after creating the smallest possible SSCCE, I still am not quite sure why this doesn't function:
    public class EmptyType {
        int testInt = 1;
    }

    import java.util.*;
     
    public class ArrayListTest {
        private ArrayList list;
     
        public ArrayListTest() {
            list.add(new EmptyType());
        }
     
        public EmptyType GetFirstIndex() {
            return list.get(0);
        }
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: ArrayList.get() Returning "Incompatible Type"

    The code defines an ArrayList without specifying a type - in so doing the get method will return an Object. Specify the type of the ArrayList using Generics (see Lesson: Generics (Updated) (The Java™ Tutorials > Learning the Java Language) ) or cast to the appropriate class before returning the object.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Destro (June 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList.get() Returning "Incompatible Type"

    Solved my problem perfectly, thank you. Only glanced at generics up until now, and didn't know they applied to a situation like this. I assumed that the type didn't matter since the compiler had no problems loading the ArrrayList with arbitrary different objects; I thought that I was the one responsible for making sure that I knew what type of object was stored in the list and only performing operations on that type of object.

Similar Threads

  1. "Selection does not contain a main type"
    By siowmotion in forum Java IDEs
    Replies: 6
    Last Post: February 15th, 2012, 04:39 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  3. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 09:24 PM
  4. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  5. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM