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

