Home > Default > Add a class into LinkedList

Add a class into LinkedList

October 11Hits:0
Advertisement
when i want to add object to linkedlist i use
LinkList.add(MyObject)
its succed but when i want to get it by use this fuction
public MyObject GetIt ( ) {
return this.removeFirst();
its give me uncompataple format;
how i can get this object after i insert it in linked ist by using function ;

Answers

Because the List doesn't know about your class (because it was written before you wrote MyObject) it instead manipulates Object references. Since everything is an Object (by inheritance) that's always valid when passing the reference in.
That is to say, a reference to a MyObject IS a reference to an Object. So...
list.add(myObject);...is always guaranteed to work. On the other hand, the reverse is not true. An Object IS NOT ALWAYS a MyObject. So when you get it out, it might not work.
You have to "cast" (tell the compiler that the particular Object that you're getting a reference to IS a MyObject) the reference thus...
return (MyObject)list.removeFirst();...which will now work in your code.
Caution - you don't understand the difference between Objects, references, types and classes (or you would not have asked this question). It is impossible to program in Java without learning this, so the sooner you do so the sooner you will be competent and comfortable. I recommend revising these distinctions in a good learner book on Java.
Dave.

Read other 8 answers

Tags:

Related Articles

  • Add a class into LinkedListOctober 11

    when i want to add object to linkedlist i use LinkList.add(MyObject) its succed but when i want to get it by use this fuction public MyObject GetIt ( ) { return this.removeFirst(); its give me uncompataple format; how i can get this object after i in

  • Java's LinkedList ClassNovember 30

    I'm having trouble just starting a linked list. How do I use this class? and how do I add an element to the list? I know this class is in the java.util.LinkedList librairy. Thanks for the help...Here is the code I got so far, am I on the right track?

  • Linkedlists problemNovember 30

    Hi, I'm trying to add objects into a LinkedList in such a way that they are always kept in alphabetical order. But im having problems and errors galore. Here is the code i have so far: public void add(Account a)         if (accounts == 0)            

  • Switching between LinkedList and ArrayNovember 30

    Hi, I'm new to OOP and Java so please bear with me. I've been given an assignment to code my own collection similar to the Java Collection. It has to have sorted arrays, unsorted arrays, sorted linked lists and unsorted linked lists. Basically, the u

  • LinkedList parameter problemNovember 30

    I created a web services application a few months ago and I am now trying to add a new method that contains a LinkedList as an input parameter. I used the weblogic.webservice.clientgen command to create the needed Codec and Holder classes for a strin

  • Fatal Error: missing resource: java.util.PropertyResourceBundle =UrgentOctober 11

    Hi all, In the JSP ,if I include the statement <%@ page import="jack.samples.*" %> it is throwing following error: This is the Error Message from the Exception:      Server caught unhandled exception from servlet [BaseServlet]: Server caug

  • Frequency of words in ArraysOctober 11

    Dear Java Folks, I still need your help! I am still having problem after succeeding in tokenizing the Strings in LinkedList. What I wanted to do is to find the Frequency of words or characters in an Array. E.g 1�A,C,F,G,A 2�Baby,H,K,A,C,K 3�Oslo,S,F,

  • Linked List inside a classOctober 11

    I would like to define a LinkedList of feet in my table class. I would like the ability to create 1000 tables some with 20 legs all different shapes and colours ect. ect. This is just an example (i'm not really creating tables) :) but its what i coul

  • "Name Clash: Method in Two Classes Have Same Erasure, Yet Neither Over-..."November 30

    Ok, I'm getting this message when trying to compile the classes below: "FileUtils.java:1117: name clash: add(E) in java.util.ArrayList<java.util.LinkedList<F>> and add(T) in gov.sandia.gajanik.util.Addable<F> have the same erasure,

  • Runtime Error displaying Linked ListNovember 30

    Hi, I've got a main class with a linked list which I am trying to display but I seem to be getting a "Class Cast Exception" eventhough I type cast the list as a String. Can anyone help please. The code is below: import java.util.*; public class

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.625 s.