incompality type

I am using link list to do a calculation.

After adding data to the list, I am going to retrieve them.

The data are int, double type etc.

I use the general form like "Object"

But something wrong!

Need help! Thanks!

package linklist;

publicclass ListTest

{

publicstaticvoid main( String args[])

{

List list1 =new List();

List list2 =new List();//create the List container

// initializer list specifies the value for each element

int array1[] ={ 160, 591, 114, 229, 230, 270, 128, 1657, 624, 1503};

double array2[] ={ 15, 69.9, 6.5, 22.4, 28.4, 65.9, 19.4, 198.7, 38.8, 138.2};

int b1[];// declare array named b1

b1 =newint[20];// create the space for b1

double b2[];//declare array named b2

b2 =newdouble[20];// create the space for b2

//insert numbers in list

for (int c1 = 0; c1 < array1.length; c1++ )

list1.insertAtFront(array1[ c1 ]);

for (int c2 = 0; c2 < array2.length; c2++)

list2.insertAtFront(array2[ c2 ]);

for (int c1 = 0; c1 < array1.length; c1++ )

{

Object removeObject = list1.removeFromFront();

b1 [ c1 ] = removeObject;

// [b]wrong[/b]

}

for (int c2 = 0; c2 < array2.length; c2++ )

{

Object removeObject = list2.removeFromFront();

b2 [ c2 ] = removeObject;

// [b]wrong here,please advise![/b] }

}

package linklist;

// ListNode and List class definitions.

// class to represent one node in a list

class ListNode

{

// package access members; List can access these directly

Object data;

ListNode nextNode;

// constructor creates a ListNode that refers to object

ListNode( Object object )

{

this( object,null );

}// end ListNode one-argument constructor

// constructor creates ListNode that refers to

// Object and to next ListNode

ListNode( Object object, ListNode node )

{

data = object;

nextNode = node;

}// end ListNode two-argument constructor

// return reference to data in node

Object getObject()

{

return data;// return Object in this node

}// end method getObject

// return reference to next node in list

ListNode getNext()

{

return nextNode;// get next node

}// end method getNext

}// end class ListNode

// class List definition

publicclass List

{

private ListNode firstNode;

private ListNode lastNode;

private String name;// string like "list" used in printing

// constructor creates empty List with "list" as the name

public List()

{

this("list" );

}// end List no-argument constructor

// constructor creates an empty List with a name

public List( String listName )

{

name = listName;

firstNode = lastNode =null;

}// end List one-argument constructor

// insert Object at front of List

publicvoid insertAtFront( Object insertItem )

{

if ( isEmpty() )// firstNode and lastNode refer to same object

firstNode = lastNode =new ListNode( insertItem );

else// firstNode refers to new node

firstNode =new ListNode( insertItem, firstNode );

}// end method insertAtFront

// insert Object at end of List

publicvoid insertAtBack( Object insertItem )

{

if ( isEmpty() )// firstNode and lastNode refer to same Object

firstNode = lastNode =new ListNode( insertItem );

else// lastNode's nextNode refers to new node

lastNode = lastNode.nextNode =new ListNode( insertItem );

}// end method insertAtBack

// remove first node from List

public Object removeFromFront()throws EmptyListException

{

if ( isEmpty() )// throw exception if List is empty

thrownew EmptyListException( name );

Object removedItem = firstNode.data;// retrieve data being removed

// update references firstNode and lastNode

if ( firstNode == lastNode )

firstNode = lastNode =null;

else

firstNode = firstNode.nextNode;

return removedItem;// return removed node data

}// end method removeFromFront

[8558 byte] By [ardmorea] at [2007-11-26 15:38:18]
# 1
What happens when you run it? What are you hoping to happen when you run it?PS
puckstopper31a at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...
# 2
Dare I ask what' swrong with java.util.LinkedList?You need to cast your Object back into a double.
es5f2000a at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...
# 3
please post the statcktrace
manuel.leiriaa at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...
# 4
I used netbean 5.5.just a red cross displaying at b1 [ c1 ] = removeObjectthen a pop up alert:incompatible typesfound: java.lang.Objectrequired: int
ardmorea at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...
# 5
cast your Object back into a double.Yes, i think so,how to cast it?Thanks
ardmorea at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...
# 6

> I used netbean 5.5.

> just a red cross displaying at

> b1 [ c1 ] = removeObject

> then a pop up alert:

> > incompatible types

> found: java.lang.Object

> required: int

>

try to cast your object to Integer and then do a intValue() on your Integer

manuel.leiriaa at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...
# 7

> I used netbean 5.5.

> just a red cross displaying at

> b1 [ c1 ] = removeObject

> then a pop up alert:

> > incompatible types

> found: java.lang.Object

> required: int

>

Integer x = (Integer)removeObject;

int y = x.intValue();

manuel.leiriaa at 2007-7-8 21:56:25 > top of Java-index,Java Essentials,Java Programming...