Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

is this correct?

Thread view: 
jinto12 - 01 Feb 2007 02:46 GMT
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.
Farcus Pottysquirt - 01 Feb 2007 02:57 GMT
> hi i was wondering if this is the correct way to initialize an
> arrayof
[quoted text clipped - 8 lines]
> 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.
Andreas Leitgeb - 01 Feb 2007 09:32 GMT
>> 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[i]=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.
Farcus Pottysquirt - 01 Feb 2007 16:36 GMT
> 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?
Andreas Leitgeb - 01 Feb 2007 18:49 GMT
>> 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.
Farcus Pottysquirt - 02 Feb 2007 02:33 GMT
> That is exactly what I meant with "can also given through a variable":
>
[quoted text clipped - 5 lines]
>   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.
Daniel Pitts - 02 Feb 2007 17:49 GMT
On Feb 1, 6:33 pm, Farcus Pottysquirt <where_is_my_...@movies.net>
wrote:

> > That is exactly what I meant with "can also given through a variable":
>
[quoted text clipped - 14 lines]
>
> 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.
Christian - 01 Feb 2007 20:38 GMT
Farcus Pottysquirt schrieb:
>> It wasn't about constructing LinkedList's, but about an array of those.
>>
[quoted text clipped - 8 lines]
> 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
Daniel Pitts - 01 Feb 2007 19:00 GMT
> hi i was wondering if this is the correct way to initialize an
> arrayof
[quoted text clipped - 8 lines]
> 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>());


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.