Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: i want to print the contents of the array, can you help?

  1. #1
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default i want to print the contents of the array, can you help?

    Hi, i want to print the contents of the array but it seems to be printing the memory address i think.
    output: [I@55f33675

    Thanks for your help and suggestions.


    public class List{
    int [] a;
    int lastItem;

    public List(){
    a=new int[10];
    a[0]=1;
    a[1]=2;
    a[2]=3;
    lastItem=-1;
    }

    public void insertItem(int newItem, int location){
    int i;

    for(i=lastItem; i>=location; i--){
    a[i+1]=a[i];
    }

    a[location]=newItem;
    lastItem++;
    }

    public int[] print(){
    return a;
    }
    }


    public class UseList {
    public static void main(String[] args){
    List l = new List();
    l.insertItem(4,1);
    int [] x = l.print();
    System.out.println(x);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: i want to print the contents of the array, can you help?

    That's the default toString() value returned by an int array (which you can tell by the [I part at the start of the String).

    Consider either iterating through the array with a for loop and printing out each item, or using the java.util.Arrays.toString(myArray) method.

  3. #3
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    Any help would be greatly appreciated.
    Thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: i want to print the contents of the array, can you help?

    My post above did not show initially but now thanks to Norm is visible. Please let me know if it helps or confuses!

  5. #5
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    The for loop worked thanks for your advice.

    Im interested in the java.util.Arrays.toString(myArray) method. How would i go about implementing this?

    Thank you for your help!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: i want to print the contents of the array, can you help?

    Quote Originally Posted by forms View Post
    The for loop worked thanks for your advice.

    Im interested in the java.util.Arrays.toString(myArray) method. How would i go about implementing this?

    Thank you for your help!
    In your initial post where you try to print out the array, try using this method instead, passing in to it your array.

  7. #7
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    Thanks so much for your help it worked!
    Last edited by forms; 11-05-2011 at 09:53 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: i want to print the contents of the array, can you help?

    You're quite welcome!

  9. #9
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    By the way when i call the insertItem method and pass in the newItem and location, it replaces the existing value at that location rather than shifting the values along and making space for the new value.
    Can you see where i am going wrong?

    Also how do i post code properly on this forum so it is numbered and formatted correctly?

    Thank you.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: i want to print the contents of the array, can you help?

    Please post your latest code.

    To show formatted code, you'll want to place the tag [code] above your code block and the tag [/code] below the block. Note that the two tags are different, use square brackets and that the second one has a slash "/" at the beginning.

    Luck!

  11. #11
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    you can use Iterator to iterate through the list.....

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: i want to print the contents of the array, can you help?

    Java Code:
    public List(){
        a=new int[10];
        a[0]=1;
        a[1]=2;
        a[2]=3;
        lastItem=-1;
    }
    I'm not sure about that lastItem=-1...

  13. #13
    ttInject is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    Yes, since your lastItem is initialized to -1, this loop:

    Java Code:
    for(i=lastItem; i>=location; i--){
      a[i+1]=a[i];
    }
    doesn't run. It goes straight to
    Java Code:
     a[location]=newItem;

  14. #14
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    So this is my code and output below
    Output: 1 4 3 0 0 0 0 0 0 0
    As you can see from the output the number 4 has replaced the existing number 2 rather than shifting it along.
    Can you see what i have done wrong?
    Thanks

    Java Code:
    public class List{
    	int [] a;
    	int lastItem;
    	
    	public List(){
    		a=new int[10];
    		a[0]=1;
    		a[1]=2;
    		a[2]=3;
    		lastItem=-1;
    	}
    	
    	public void insertItem(int newItem, int location){
    		int i;
    		
    		for(i=lastItem; i>=location; i--){
    			a[i+1]=a[i];
    		}
    		
    		a[location]=newItem;
    		lastItem++;
    		
    	}
    
    	public void print() {
    		for(int i=0; i<a.length; i++){
    			System.out.print(a[i] + " ");
    		}
    		
    	}
    
    }
    Java Code:
    public class UseList {
    	public static void main(String[] args){
    		List l = new List();
    		l.insertItem(4,1);
    		l.print();
    	}
    }

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: i want to print the contents of the array, can you help?

    Can you see what i have done wrong?
    Did you read replies #12 and #13? If they aren't clear (especially ttInject's), say what it is about them that you don't understand.

  16. #16
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    The for loop runs when i assign 8 to lastItem, and the numbers shift correctly. I understand the logic behind it now.
    Thank you.

    Also if i needed a crash course in data structures using java can you recommend any good websites?

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: i want to print the contents of the array, can you help?

    I find the Collections Framework and use of Generics sufficient. (The links are to Oracle's Tutorial)

    If you want to understand how these things are implemented, then "google: java data structures" turns up lots of pages. Often notes from university courses llike this one from the UK.

  18. #18
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
    public List(){
        a=new int[10];
        a[0]=1;
        a[1]=2;
        a[2]=3;
        lastItem=-1;
    }
    I'm not sure about that lastItem=-1...
    My code is now working but i originally copied this code from youtube - CS 61B Lecture 7: Linked Lists 1
    The lecturer in this video at 3:20 uses lastItem = -1;
    Could someone help me to understand why he uses -1?
    As using -1 doesnt allow the for loop to run.

    Thanks for your help.

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: i want to print the contents of the array, can you help?

    Good question. We are talking about CS 61B Lecture 7: Linked Lists I - YouTube

    At 3:20: "So, here it is. Here's my List class":

    Java Code:
    public class List {
        int[] a;
        int lastItem;
    
        public List() {
            a = new int[10];
            lastItem = -1;
        }
    
        public void insertItem(int newitem, int location) {
            int i;
            
            for(i = lastitem; i >= location; i--) {
                a[i + 1] = a[i];
            }
            a[location] = newitem;
            lastitem++;
        }
    }
    The lecturer explains that "there's an int called lastItem whose job is simply to be an index to the last item of the last ... lastItem is the index of the last item."

    Look at the constructor. The lecturer doesn't really talk about this because he moves on to linked lists. To begin with the list has no no items put into it. So, what should lastItem be? It can't be zero because zero is not the index of the last item in the list and that's because ... there are no items in the list!

    Basically he makes a special case out of this. His comment shortly after 3:20 should really be amended to something like "lastitem is the index of the last item, unless there are no items in which case it is -1." -1 is a good choice because when you do add an item it will be incremented to zero which is the correct index of the last (and only) element in the list.

    Now, back to your code...

    Your list is not empty. It begins life (ie just after the constructor has finished) with three items in it. So what is the index of the last item? (Hint: it's 2) And that's exactly what lastItem should be set to. The lecturer's example created an empty list (lastItem of -1): yours creates an item with three things in it (lastItem of 2).

    Assigning 8 to lastitem will wrok (sorta/kinda) but will fail when you try and append an item to the end of the list or do anything else that depends on lastitem being the real, actual, index of the last list item.
    Last edited by pbrockway2; 11-10-2011 at 12:58 AM.
    forms likes this.

  20. #20
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: i want to print the contents of the array, can you help?

    Thank you so much for your explaination. Very helpful.
    Do you know of any simple java programs that use a linked list that i could copy out and run?

Page 1 of 2 12 LastLast

Similar Threads

  1. contents of List<object> array changed?
    By soichi in forum New To Java
    Replies: 2
    Last Post: 01-19-2011, 10:58 AM
  2. Scanning text file and inserting contents into array
    By jmwalloh in forum New To Java
    Replies: 8
    Last Post: 03-24-2010, 12:33 PM
  3. Cant print the contents of a table
    By tudorH in forum JDBC
    Replies: 2
    Last Post: 03-20-2010, 06:53 PM
  4. Concatinating contents of a string array based on condition
    By gangsterooseven in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 07:35 AM
  5. Printing the contents of an array of objects
    By Mr.Paplu in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 04:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •