Results 1 to 11 of 11
- 08-25-2009, 11:07 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
cannot find symbol for "list.addFirst"
Can somebody help me with this code..
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListPerformanceDemo
{
final int SIZE = 100000;
public ListPerformanceDemo()
{
System.out.println(Integer.MAX_VALUE + "\n\n");
ArrayList<String> arrayL = new ArrayList<String>();
System.out.println("time for ArrayList = " + timeAddFrontofList(arrayL));
LinkedList<String> linkedL = new LinkedList<String>();
System.out.println("time for LinkedList = " + timeAddFrontofList(linkedL));
}
//Add object to the front of the list using addFirst
long timeAddFrontofList(List list)
{
long start = System.currentTimeMillis();
for (int i = 0; i < SIZE; i++)
{
String s = new String(i + " ");
// add object to the front of the list
list.addFirst(s);
}
return System.currentTimeMillis() - start;
}
public static void main(String args[])
{
new ListPerformanceDemo();
}
}//end class
apparently this code will work if i change it to list.add(0,s), but i want to use list.addFirst(s)..
what should i change??
:confused::confused:
- 08-25-2009, 11:15 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
owh..
and i actually want to add new object on the at the front of the list..
-
What is this addFirst method? Where in the API have you seen this? You know that you can't just make up a method for an existing class and expect mathemagically that it will work.
- 08-25-2009, 11:34 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
addFirst is a method in LinkedList. You are trying to call it on an ArrayList which is not a LinkedList. Better have the API specs open as you are coding.
- 08-25-2009, 11:35 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
DataStructures
Class LinkedList
java.lang.Object
|
+--DataStructures.LinkedList
public class LinkedList
extends java.lang.Object
A simple implementation of singly linked lists.
Lists are created empty, for example:
LinkedList list = new LinkedList();
after which items can be inserted at the front of the list by
list.addFirst(item)
-retrieve from website www[dot]informatics[dot]susx[dot]ac[dot]uk/courses/dats/DataStructures/docs/DataStructures/LinkedList
-
Wrong info -- deleted
Last edited by Fubarable; 08-25-2009 at 11:49 PM.
-
- 08-25-2009, 11:57 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
- 08-26-2009, 12:06 AM #9
You have this method:
It is saying you have a variable named 'list' of class 'List'. The class List does not have a method 'addFirst' only the class LinkedList does.Java Code:long timeAddFrontofList(List list) { ... }
Specs:
List
LinkedList
ArrayListLast edited by mrmatt1111; 08-26-2009 at 12:14 AM.
My Hobby Project: LegacyClone
- 08-26-2009, 12:25 AM #10
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
owh..
no wonder..
change List -> LinkedList..
problem solved..
thanks everyone..
:):)
- 08-26-2009, 08:55 AM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
By specs I meant API specs. Here is a sample. Programming without those is like shooting in the dark. You could even shoot your own foot.
Similar Threads
-
Compiling/Running Project in Command Line: "Could not find main class" Error
By Yasemin Gokce in forum New To JavaReplies: 1Last Post: 06-30-2009, 02:32 PM -
Reading strings with like "degree symbol" - HOW?
By RR_QQ in forum New To JavaReplies: 13Last Post: 02-18-2009, 02:16 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Cannont find symbol Constructor" error
By Welsh in forum New To JavaReplies: 7Last Post: 01-25-2008, 12:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks