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[i] = 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
Lasse Reichstein Nielsen - 27 Sep 2007 13:13 GMT
> 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:
>
[quoted text clipped - 5 lines]
> 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.e., do create a SSCCE (<URL:http://www.physci.org/codes/sscce.html>)
that displays the problem.
> 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.
[quoted text clipped - 13 lines]
> 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[i] = 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

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Lew - 27 Sep 2007 14:56 GMT
Taria writes:
>> ... put objects that were linked lists
>> in the array with the cell of the array pointing to the head of each
>> linked list.
<http://java.sun.com/docs/books/tutorial/index.html>

Signature
Lew
Roedy Green - 27 Sep 2007 22:39 GMT
>> 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.)
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".

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lasse Reichstein Nielsen - 28 Sep 2007 00:08 GMT
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

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Roedy Green - 28 Sep 2007 00:28 GMT
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 29 Sep 2007 02:42 GMT
>> 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
[quoted text clipped - 3 lines]
> 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
Roedy Green - 10 Oct 2007 00:18 GMT
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Stefan Ram - 10 Oct 2007 00:40 GMT
>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.
Lasse Reichstein Nielsen - 11 Oct 2007 06:08 GMT
>>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.
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

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Roedy Green - 11 Oct 2007 06:11 GMT
> 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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
nebulous99@gmail.com - 28 Sep 2007 03:56 GMT
> 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. ;)
Brian - 27 Sep 2007 15:42 GMT
>String [] bucket; // this is my array to hold the objects
>for (int i = 0;i< N;i++){
> bucket[i] = 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
Roedy Green - 27 Sep 2007 22:28 GMT
>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.)
see http://mindprod.com/jgloss/array.html
Allocation is only one of the things you need to know about arrays.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 27 Sep 2007 22:37 GMT
>The error I get is non static variable cannot be referenced from a
>static context
see
http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Taria - 28 Sep 2007 06:33 GMT
On Sep 27, 11:37 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> >The error I get is non static variable cannot be referenced from a
> >static context
[quoted text clipped - 3 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
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)
Mark Rafn - 28 Sep 2007 09:22 GMT
>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[i] = 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.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Roedy Green - 11 Oct 2007 06:10 GMT
>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.
see http://mindprod.com/jgloss/array.html
for sample code.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com