Re: Cannot find symbol java error (newbie question)
- From: Roedy Green <see_website@xxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Sep 2007 11:12:19 GMT
On Sat, 29 Sep 2007 02:34:22 -0700, Taria <mchew02@xxxxxxxxxxx> wrote,
quoted or indirectly quoted someone who said :
import java.util.*;
import java.lang.*;
public class myProg{
public static void main(String[] arguments) {
int size = 10; // arbritray size - test number
LinkedList [] bucketArray = new LinkedList[size];
// next try to insert the value into bucket array
//bucketArray[1] = array[0]; incompatible types (my first
attempt to assign a value :p)
Integer four = new Integer(4); //so now four is an object
bucketArray.insert(four,0); //cannot find symbol
} //end of main
class LinkedList{
private Node head;
private int length;
public LinkedList() {
this.head = null;
this.length = 0;
}
public void insert (Object data,int position){
System.out.println ("Node code here\n");
}
}
I have corrected your program as follows.
import java.util.*;
import java.lang.*;
/** shows compiler error message */
public class MyProg
{
public static final int MAX_BUCKETS = 10; // arbitrary size - test
number
public static void main(String[] arguments)
{
// create an array of LinkedLists
LinkedList [] bucketArray = new LinkedList[ MAX_BUCKETS ];
// next insert value into bucket array
// bucketArray uses array[] syntax, the LinkList contents use
method syntax.
Integer four = new Integer(4); //so now four is an object
// need to create an LinkedList to add to the array, not an
Object
LinkedList ll = new LinkedList();
ll.insert ( four, 0 );
// now add the linked list to the array.
bucketArray[0] = ll;
} //end of main
}
/** top level class to implement a LinkedList */
class LinkedList
{
private Node head;
private int length;
public LinkedList()
{
this.head = null;
this.length = 0;
}
public void insert (Object data,int position)
{
System.out.println ("Node code here\n");
}
}
/** SSCCE was missing a node class. We provide a dummy one */
class Node
{
}
You are confused between arrays and Lists. You rarely need both.
You could either have an array of objects or a LinkedList of Objects,
but you normally would not have an array of LinkedLists of Objects.
See http://mindprod.com/jgloss/array.html
http://mindprod.com/jgloss/arraylist.html
LinkedLists in general are rare since they are slower than other sorts
of lists most of the time. The beauty of the List interface is you can
try several list implementations to see which actually works best with
minimal code change.
Normally you would have an ArrayList<Dog> where Dog is some specific
class. It is rare to see a List<Object> any more.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.
- Follow-Ups:
- References:
- Cannot find symbol java error (newbie question)
- From: Taria
- Cannot find symbol java error (newbie question)
- Prev by Date: Re: Method.invoke() with one arg compiles despite method signature
- Next by Date: Re: Method.invoke() with one arg compiles despite method signature
- Previous by thread: Re: Flame war! [was: Cannot find symbol java error (newbie question)]
- Next by thread: Re: Cannot find symbol java error (newbie question)
- Index(es):
Relevant Pages
|
|