Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java pointers

  1. #1
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Exclamation Java pointers

    Hi I am currently in my second year studying computer science and the assignment I have been assigned to complete is one in which I am required to create a chat room server. I have been really bad at doing work and now I am in need of some help.

    The first part of the assignment which I am having trouble with asks me to create a pointer in a class chatter (already been implemented) which points towards the next chatter in a list of chatters, depending on whether or not said chatter is in the 'alive' or 'dead' list of chatters (these two lists need to be distinguished to save space).

    I understand that on these types of forums it is not customary to give answers out, so I would really appreciate at least some guidance on the matter if anyone is willing to help. I can also post the code which I am meant to be working with up here if that would make it easier, thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java pointers

    points towards the next chatter in a list
    Given a list or array, an index(pointer) to the list "points" to one element in the list. To get the next element in the list, increment the index by one.
    If the list is a linked list, then the pointer would be a reference to an element in the list. The next element in the list would be found by loading the pointer/reference from the variable in the element that points to the next element.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    bassie (November 29th, 2012)

  4. #3
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Java pointers

    Hi Norm, thanks for the reply!
    I believe the list is a linked list, but I have no idea how to actually tell the program to point towards an actual element in the list... This is what I've done so far;

    ...

    public void chatPointer (Chatter p)
    {
    ChatterList cl = new ChatterList (); //instantiates a copy of the list

    Chatter p = cl.theList //variable for traversing the list

    ...

    I added this code to the class Chatter where the pointer needs to be stored, should I be adding a loop or is it something as simple as Chatter p = thelist.next ?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java pointers

    point towards an actual element in the list
    In the posted code, the variable cl points to an object.
    If next is a reference to the next node in the list, then p will point to that node.

    Without definitions for the variables (the code), I can only guess what values will be in the variables after the assignment statements you posted are executed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    bassie (November 29th, 2012)

  7. #5
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Java pointers

    Well in the example code provided by the lecturer it appears that next is just used as a command to give the next element in a list, as he never actually defines what 'next' should do anywhere else in the code., this is the first usage of it;

    public void broadcast(String msg)
    {
    // variable for traversing the linked list
    Chatter tmp = theList;

    /*
    * traverse the list
    */
    while (tmp != null) // exit loop at the end of the list
    {
    // send the message
    tmp.theChatter.sendToUser(msg);
    // and move on to the next chatter in the list
    tmp = tmp.next;
    }
    }
    I am so confused right now.. is there any website you could recommend which has tutorials on java which could bring me to at least this level of understanding? Thanks again for your reply!

    (this code is in class ChatterList which is the actual implementation of the list)

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java pointers

    The only link I have is to the java tutorial:
    Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
    The Really Big Index
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    bassie (November 29th, 2012)

Similar Threads

  1. pointers
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 23rd, 2012, 01:56 PM
  2. Living Without cin... or Pointers (A Thread About Java Text Adventures!)
    By chaucer345 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 17th, 2012, 07:54 PM
  3. Have a C++ project due soon and am not sure if I'm using pointers right.
    By javapenguin in forum Other Programming Languages
    Replies: 12
    Last Post: September 19th, 2011, 09:54 PM
  4. Null pointers
    By Jared in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 7th, 2010, 06:40 PM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 08:52 AM

Tags for this Thread