Creating an array of objects (newbie question)

Discussion in 'Java' started by Taria, Sep 27, 2007.

  1. Taria

    Taria Guest

    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
     
    Taria, Sep 27, 2007
    #1
    1. Advertising

  2. Taria <> writes:

    > 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.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.
    >
    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 27, 2007
    #2
    1. Advertising

  3. Taria

    Lew Guest

    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>

    --
    Lew
     
    Lew, Sep 27, 2007
    #3
  4. Taria

    Brian Guest

    On Thu, 27 Sep 2007 04:50:13 -0700, Taria <> wrote:


    >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
     
    Brian, Sep 27, 2007
    #4
  5. Taria

    Roedy Green Guest

    >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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 27, 2007
    #5
  6. Taria

    Roedy Green Guest

    Roedy Green, Sep 27, 2007
    #6
  7. Taria

    Roedy Green Guest

    On Thu, 27 Sep 2007 14:15:36 +0200, Lasse Reichstein Nielsen
    <> wrote, quoted or indirectly quoted someone who said :

    >> 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".
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 27, 2007
    #7
  8. Roedy Green <> writes:

    > On Thu, 27 Sep 2007 14:15:36 +0200, Lasse Reichstein Nielsen
    > <> wrote, quoted or indirectly quoted someone who said :


    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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 28, 2007
    #8
  9. Taria

    Roedy Green Guest

    On Fri, 28 Sep 2007 01:09:38 +0200, Lasse Reichstein Nielsen
    <> wrote, quoted or indirectly quoted someone who said :

    >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.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 28, 2007
    #9
  10. Taria

    Guest

    On Sep 27, 8:15 am, Lasse Reichstein Nielsen <> wrote:
    > 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. ;)
     
    , Sep 28, 2007
    #10
  11. Taria

    Taria Guest

    On Sep 27, 11:37 am, Roedy Green <>
    wrote:
    > On Thu, 27 Sep 2007 04:50:13 -0700, Taria <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    > >The error I get is non static variable cannot be referenced from a
    > >static context

    >
    > seehttp://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF
    > --
    > 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)
     
    Taria, Sep 28, 2007
    #11
  12. Taria

    Mark Rafn Guest

    Taria <> wrote:
    >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.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Sep 28, 2007
    #12
  13. Roedy Green wrote:
    > On Fri, 28 Sep 2007 01:09:38 +0200, Lasse Reichstein Nielsen
    > <> wrote, quoted or indirectly quoted someone who said :
    >> 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.


    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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Sep 29, 2007
    #13
  14. Taria

    Roedy Green Guest

    On Fri, 28 Sep 2007 21:42:50 -0400, Arne Vajhøj <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >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.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 10, 2007
    #14
  15. Taria

    Stefan Ram Guest

    Roedy Green <> writes:
    >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.
     
    Stefan Ram, Oct 10, 2007
    #15
  16. -berlin.de (Stefan Ram) writes:

    > Roedy Green <> writes:
    >>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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 11, 2007
    #16
  17. Taria

    Roedy Green Guest

    On Thu, 27 Sep 2007 04:50:13 -0700, Taria <> wrote,
    quoted or indirectly quoted someone who said :

    >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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 11, 2007
    #17
  18. Taria

    Roedy Green Guest

    On 9 Oct 2007 23:40:13 GMT, -berlin.de (Stefan Ram) wrote,
    quoted or indirectly quoted someone who said :

    > 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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 11, 2007
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anton Ishmurzin
    Replies:
    5
    Views:
    508
    Anton Ishmurzin
    Apr 7, 2004
  2. Jeff

    [newbie] creating array in C#

    Jeff, Mar 6, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    298
    Mark Rae
    Mar 6, 2007
  3. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
  4. News123
    Replies:
    7
    Views:
    383
    Dave Angel
    Jun 29, 2009
  5. Einar Høst
    Replies:
    11
    Views:
    187
    Einar
    Mar 9, 2006
Loading...

Share This Page