Creating an array of objects (newbie question)

T

Taria

Hello all,

I hope I"m posting this in the right place. Java is somewhat new to
me, and I'm not really comfortable with handling objects, hence my
confusion.

I'm trying to create an array of size N with objects that will be
linked lists in the array (essentially doing a bucket sort and the
requirement is to be able to create linked lists in a bucket in the
event of a collision.)

In my brilliance, I figured I could put objects that were linked lists
in the array with the cell of the array pointing to the head of each
linked list. (just humor me, I'm trying to initialize an array with
an object to start, never mind the sorting part for now.)

This is the code I tried:

String [] bucket; // this is my array to hold the objects
for (int i = 0;i< N;i++){
bucket = new LinkedList(); //linked list is my object
}

The error I get is non static variable cannot be referenced from a
static context.

I understand that there is a conflict of a primitive variable
reference to an object reference, but is it referring to the bucket
array as a static? Does this mean I need to declare the array to be
an object, too? :x

I pulled this code off someone else's msg that said this would work
and I have no doubt of that, but I don't understand it.

public class AnyObj {
public Object[] someMethod( )
{
AnyObj arrayOfObj[] = new AnyObj[5];
arrayOfObj[0] = new AnyObj();
arrayOfObj[4] = new AnyObj();
return arrayOfObj;
}

My question here is this appears to be an array of AnyObj, in my case,
if I were to substitute my array name, then each cell would become a
part of the Linked list object? (sorry if i'm not making sense, i'm
delirious, it's late, i'm tired and i've been working on this for a
long time.) If my linked list object had a head and node, wouldn't
the array have that too if I declared it to be an object linked list?
Confusion couldn't be finer right now.

And finally, am I approaching this problem the right way? How
plausible is an array of linked lists? I mean, is Java capable?

Any thoughts or hints are appreciated.

-m

if only they made compilers newbie friendly. :p
 
L

Lasse Reichstein Nielsen

Taria said:
In my brilliance, I figured I could put objects that were linked lists
in the array with the cell of the array pointing to the head of each
linked list. (just humor me, I'm trying to initialize an array with
an object to start, never mind the sorting part for now.)

No need to humor anything. This is perfectly rational.
This is the code I tried:

String [] bucket; // this is my array to hold the objects
for (int i = 0;i< N;i++){
bucket = new LinkedList(); //linked list is my object
}

The error I get is non static variable cannot be referenced from a
static context.


Well, I guess we would like to see the context then.
I understand that there is a conflict of a primitive variable
reference to an object reference, but is it referring to the bucket
array as a static?

The problem is that code in a static context (a static method, e.g.,
main) refers to a variable/field that is not declared static. This
is not allowed.
Does this mean I need to declare the array to be an object, too? :x

All arrays are objects. The only non-object types in Java are the
number types (byte, short, char, int, long, float, double) and boolean.
I pulled this code off someone else's msg that said this would work
and I have no doubt of that, but I don't understand it.

public class AnyObj {
public Object[] someMethod( )
{
AnyObj arrayOfObj[] = new AnyObj[5];
arrayOfObj[0] = new AnyObj();
arrayOfObj[4] = new AnyObj();
return arrayOfObj;
}

My question here is this appears to be an array of AnyObj, in my case,
if I were to substitute my array name, then each cell would become a
part of the Linked list object? (sorry if i'm not making sense, i'm
delirious, it's late, i'm tired and i've been working on this for a
long time.)

You are not making much sense :)

If you create an array of references to objects of a specific type,
each element of the array is initially null.
You can then create objects and put references to them in the array
in whatever way you want.
If my linked list object had a head and node, wouldn't
the array have that too if I declared it to be an object linked list?
Confusion couldn't be finer right now.

Wipe that thought and restart :)

I think I might be able to guess what the confusion comes from.

The type "LinkedList[]" is an array type. Its instances are
just arrays, they are not LinkedList's.
I.e., "new LinkedList[5]" will not create any LinkedList objects,
and it will not have any methods or properties from the LinkedList
class.
Each element of that array is a reference to a LinkedList
object. Initially that reference is null.

LinkedList[] theArray; // declare variable
theArray = new LinkedList[n]; // create array object of size n.
// No LinkedList object created yet
for(int i = 0; i < n; i++) {
theArray = new LinkedList(); // create LinkedList object and put at
// position i.
}
And finally, am I approaching this problem the right way? How
plausible is an array of linked lists? I mean, is Java capable?

Perfectly capable. You'll find that this is indeed the implementation
of java.util.HashMap.
if only they made compilers newbie friendly. :p

Compilers aren't friendly to anybody. They are heartless nitpickers
that enjoy telling you about all your mistakes. The best one can
do is to satisfy their pedantry to keep them quiet :)

/L
 
B

Brian

String [] bucket; // this is my array to hold the objects
for (int i = 0;i< N;i++){
bucket = new LinkedList(); //linked list is my object
}


Maybe this will solve your problem:

ArrayList<LinkedList<AnyObj>> bucket = new
ArrayList<LinkedList<AnyObj>>();

int n = 10;
for (int i = 0; i < n; i++) {
bucket.add(new LinkedList<AnyObj>());
}

This creates an ArrayList of LinkedList objects. And the LinkedList
contains AnyObj objects.

/Brian
 
R

Roedy Green

The linked list does not contain objects, just pointers to objects. It
does not own or incorporate the objects. Your worry is akin to
worrying what would happen to your dog if the Oxford people decided to
index the word "Rover".
 
L

Lasse Reichstein Nielsen

I didn't write it, and it was, I hope, quite clear who I was quoting.
Please retain the attribution when quoting a quote, especially when
not quoting anything from the response. Otherwise it is very easy
to get confuzed about who wrote what.
/L
 
R

Roedy Green

I didn't write it, and it was, I hope, quite clear who I was quoting.
Please retain the attribution when quoting a quote, especially when
not quoting anything from the response. Otherwise it is very easy
to get confuzed about who wrote what.

Unless someone is personally attacking someone or otherwise
misbehaving, I don't pay attention to attributions. With a few
exceptions I have no mental model of whom any of you are. I have no
idea what you look like, how old you are, where you live, your
political views, your level of experience etc. I see only questions
and ideas.

The current system of attributions is fundamentally flawed. Long ago
I suggested automating it to get the records accurately. However,
newsgroup and email protocols are as resistant to change an dinosaur
DNA. see http://mindprod.com/project/mailreadernewsreader.html

The main reason I have been leaving out attributions is a problem with
the Telus newsserver. It often won't accept posts unless I remove all
trailing blanks and also the attribution.
 
N

nebulous99

Compilers aren't friendly to anybody. They are heartless nitpickers
that enjoy telling you about all your mistakes. The best one can
do is to satisfy their pedantry to keep them quiet :)

In that regard, they resemble certain regular posters in this
newsgroup. ;)
 
T

Taria

seehttp://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF

Thanks guys...so many things to try tonight! Whee! I'm
excited...cool things to try and learn. :)

I thank you all for your helpful links and comments as I'm sure this
is only the beginning of the errors I will encounter as I progress
through this assignemnt and references are always nice.

taria (waiting for the understanding to come shining through :p been
thinking about this puzzle all day long)
 
M

Mark Rafn

Taria said:
In my brilliance, I figured I could put objects that were linked lists
in the array with the cell of the array pointing to the head of each
linked list. (just humor me, I'm trying to initialize an array with
an object to start, never mind the sorting part for now.)

No worries. You'll basically work with an array of linked lists.
This is the code I tried:
String [] bucket; // this is my array to hold the objects
for (int i = 0;i< N;i++){
bucket = new LinkedList(); //linked list is my object
}


Why is bucket an array of String, and why do you think you can put a
LinkedList object into a String element?
The error I get is non static variable cannot be referenced from a
static context.

That's not the error you get from the above snippet. You may be getting that
error for another reason (like trying to reference a non-static member
variable from method that's declared static).

The error you'll get from the above code is something like "incompatible
types - found LinkedList, required String". And if you declare "LinkedList[]
buckets;" instead of String[], you should be on your way.
I understand that there is a conflict of a primitive variable
reference to an object reference
(sorry if i'm not making sense, i'm
delirious, it's late, i'm tired and i've been working on this for a
long time.) If my linked list object had a head and node, wouldn't
the array have that too if I declared it to be an object linked list?

You seem to be confusing the array with it's contents. The array has
LinkedList objects as it's elements. The array does not have heads or nodes,
except those of the LinkedLists.
And finally, am I approaching this problem the right way? How
plausible is an array of linked lists? I mean, is Java capable?

Sounds reasonable to me. LinkedList[] is a sensible data structure.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Roedy said:
Unless someone is personally attacking someone or otherwise
misbehaving, I don't pay attention to attributions.

Correct attribution is part of proper newsgroup usage.
The main reason I have been leaving out attributions is a problem with
the Telus newsserver. It often won't accept posts unless I remove all
trailing blanks and also the attribution.

Then it is seriously broken.

Arne
 
R

Roedy Green

Correct attribution is part of proper newsgroup usage.

My IAP, Telus, does not think so. For some bizarre reason they often
refuse to accept posts unless I remove the attribution. I have written
many times and sat on hold for hours trying to get this resolved. They
have outsourced their help department to people who cannot understand
a problem unless it is on the their menu tree.
 
S

Stefan Ram

Roedy Green said:
My IAP, Telus, does not think so.
For some bizarre reason they often refuse to accept posts

An internet access provider accepts your IP packets or sends
IP packets to you. It does not operate on the NNTP layer level.

It can neither refuse nor accept post.

An NNTP server (or client) accepts or refuses posts.

A customer of an internet access provider A can either
operate his own NNTP server or use an NNTP server of an
NNTP server provider B, where A does not have to be B.

Thus, it seems that you refer to an /NSP/ -
the NNTP server provider you use.

An IAP does not have to be the NSP and vice versa.
 
L

Lasse Reichstein Nielsen

An internet access provider accepts your IP packets or sends
IP packets to you. It does not operate on the NNTP layer level.

My IAP also runs the NNTP server that I use. This is quite often
the case.
It's usually called an ISP here: Internet Service Provider. Perhaps
because they generally provide other services than a whole through
to the 'net - at least e-mail, news, and DNS.
Thus, it seems that you refer to an /NSP/ -
the NNTP server provider you use.

Which just happens to also be the IAP. Actually, I have never seen
the abbreviation "NSP" before.
An IAP does not have to be the NSP and vice versa.

But in this case it is.

If your point is that Roedy Green could, and should, use a different
NSP, if the current one does not allow him to follow common
netiquette, then I agree (in principle, personally I wouldn't want to
pay any more for my internet use than I already do), but I can't read
that into your reply.

/L
 
R

Roedy Green

An internet access provider accepts your IP packets or sends
IP packets to you. It does not operate on the NNTP layer level.

Your IAP in Canada is also your newsserver supplier.
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top