is this correct?

J

jinto12

hi i was wondering if this is the correct way to initialize an
arrayof
linked list like this

LinkedList [] Separate = new LinkedList []; ???


im importing the the linked list library from the java api. i just
need guidence if this is the correct way to initialize this type of
linked list.


i will appreciate your help.
thanks.
 
F

Farcus Pottysquirt

jinto12 said:
hi i was wondering if this is the correct way to initialize an
arrayof
linked list like this

LinkedList [] Separate = new LinkedList []; ???


im importing the the linked list library from the java api. i just
need guidence if this is the correct way to initialize this type of
linked list.


i will appreciate your help.
thanks.
Here's the section from the JavaDocs on the constructor for a linked list

http://java.sun.com/j2se/1.3/docs/api/java/util/LinkedList.html#LinkedList()

It's for 1.3 but should work for later versions.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html#LinkedList()

Here it is for 1.5 as well.

Google java linked list constructor and see what it tells you.
That's how I found those two links.
 
A

Andreas Leitgeb

Farcus Pottysquirt said:
jinto12 said:
hi i was wondering if this is the correct way to initialize an
arrayof linked list like this
LinkedList [] Separate = new LinkedList []; ???
Here's the section from the JavaDocs on the constructor for a linked list

It wasn't about constructing LinkedList's, but about an array of those.

to OP:

First, there are some conventions on style that you
had better learn (for your own benefit). One of
these is that variables shouldn't start with an
uppercase letter, unless it's static final (which yours
isn't) google "java styleguide" for more info on that.

You have to specify the size of the array: (e.g.)
LinkedList[] separate = new LinkedList[42];
The size can also given through a variable, but once the array
is created, its size is cast in reinforced concrete :)

After that, you probably need to populate it with actual
LinkedList instances. You have to code the loop yourself:
for (int i=0; i< separate.length; i++)
separate=new LinkedList( ... );
What you need instead of the dots is actually what Farcus described.

If you know in advance, what types of Objects you intend to
add to the list, and if further you're using java 1.5 or newer,
then you should have a good look at java generics.
 
F

Farcus Pottysquirt

It wasn't about constructing LinkedList's, but about an array of those.Fair enough
You have to specify the size of the array: (e.g.)
LinkedList[] separate = new LinkedList[42];
The size can also given through a variable, but once the array
is created, its size is cast in reinforced concrete :)

What if you don't know the size of the array when you are writing the code.
Is there a dynamic nature that can be applied to the array class apart from
switching to use ArrayList?
 
A

Andreas Leitgeb

Farcus Pottysquirt said:
You have to specify the size of the array: (e.g.)
LinkedList[] separate = new LinkedList[42];
The size can also given through a variable, but once the array
is created, its size is cast in reinforced concrete :)

What if you don't know the size of the array when you are writing the code.

That is exactly what I meant with "can also given through a variable":

You don't need to know at programming time, but during runtime, when
code flow reaches array-creation.

E.g. you could do:
int randomvar=(int)(1000000*Math.random());
LinkedList[] separate = new LinkedList[randomvar];
and will get an array with some random size.
Is there a dynamic nature that can be applied to the array class apart from
switching to use ArrayList?

Once the array (the one with []) is created, it's size is fixed.
For dynamic length structures see the Collection framework.
 
D

Daniel Pitts

hi i was wondering if this is the correct way to initialize an
arrayof
linked list like this

LinkedList [] Separate = new LinkedList []; ???

im importing the the linked list library from the java api. i just
need guidence if this is the correct way to initialize this type of
linked list.

i will appreciate your help.
thanks.

Is your intent to create more than one linked list?

If you only want a list of Objects, you'd be better off doing
something like this:
List<Object> list = new LinkedList<Object>();

If you truely want more than one list, you might want something like
this:
List<List<Object>> listOfLists = new ArrayList<List<Object>>();
listOfLists.add(new LinkedList<Object>());
 
C

Christian

Farcus said:
It wasn't about constructing LinkedList's, but about an array of those.
Fair enough
You have to specify the size of the array: (e.g.)
LinkedList[] separate = new LinkedList[42];
The size can also given through a variable, but once the array
is created, its size is cast in reinforced concrete :)

What if you don't know the size of the array when you are writing the code.
Is there a dynamic nature that can be applied to the array class apart from
switching to use ArrayList?
no arryas size can't be changed dynamically, thats exactly what
collections are for... nothing hinders you to use a collection of a
collection
 
F

Farcus Pottysquirt

Andreas said:
That is exactly what I meant with "can also given through a variable":

You don't need to know at programming time, but during runtime, when
code flow reaches array-creation.

E.g. you could do:
int randomvar=(int)(1000000*Math.random());
LinkedList[] separate = new LinkedList[randomvar];
and will get an array with some random size.
Once the array (the one with []) is created, it's size is fixed.
For dynamic length structures see the Collection framework.
So it doesn't matter how the size is defined, either by a variable or by
a "hard-coded value" the size of an array is fixed when it is defined at
run time.

Makes sense to me.
 
D

Daniel Pitts

Andreas said:
That is exactly what I meant with "can also given through a variable":
You don't need to know at programming time, but during runtime, when
code flow reaches array-creation.
E.g. you could do:
int randomvar=(int)(1000000*Math.random());
LinkedList[] separate = new LinkedList[randomvar];
and will get an array with some random size.
Once the array (the one with []) is created, it's size is fixed.
For dynamic length structures see the Collection framework.

So it doesn't matter how the size is defined, either by a variable or by
a "hard-coded value" the size of an array is fixed when it is defined at
run time.

Makes sense to me.

Right, the size of the array object is fixed. But, unlike c++, you
can reassign the reference to a new object.

Object[] myArray = new Object[10];
System.out.println(myArray.length);
myArray = new Object[15];
System.out.println(myArray.length);

output should be:
10
15

Note that I've created two distinctly different arrays, myArray first
points to one, and then later the other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top