Results 1 to 20 of 21
- 11-05-2011, 04:55 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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);
}
}
-
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.
- 11-05-2011, 07:05 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
Re: i want to print the contents of the array, can you help?
Any help would be greatly appreciated.
Thank you.
-
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!
- 11-05-2011, 08:54 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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!
-
Re: i want to print the contents of the array, can you help?
- 11-05-2011, 09:44 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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.
-
Re: i want to print the contents of the array, can you help?
You're quite welcome!
- 11-05-2011, 09:55 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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.
-
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-06-2011, 08:14 AM #11
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Re: i want to print the contents of the array, can you help?
you can use Iterator to iterate through the list.....
- 11-06-2011, 09:30 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: i want to print the contents of the array, can you help?
I'm not sure about that lastItem=-1...Java Code:public List(){ a=new int[10]; a[0]=1; a[1]=2; a[2]=3; lastItem=-1; }
- 11-06-2011, 11:29 AM #13
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
Re: i want to print the contents of the array, can you help?
Yes, since your lastItem is initialized to -1, this loop:
doesn't run. It goes straight toJava Code:for(i=lastItem; i>=location; i--){ a[i+1]=a[i]; }
Java Code:a[location]=newItem;
- 11-06-2011, 06:40 PM #14
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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(); } }
- 11-06-2011, 10:31 PM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: i want to print the contents of the array, can you help?
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.Can you see what i have done wrong?
- 11-07-2011, 02:42 PM #16
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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?
- 11-08-2011, 12:58 AM #17
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 11-09-2011, 06:58 PM #18
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
Re: i want to print the contents of the array, can you help?
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.
- 11-10-2011, 12:54 AM #19
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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":
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."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++; } }
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.
- 11-11-2011, 06:46 PM #20
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
contents of List<object> array changed?
By soichi in forum New To JavaReplies: 2Last Post: 01-19-2011, 10:58 AM -
Scanning text file and inserting contents into array
By jmwalloh in forum New To JavaReplies: 8Last Post: 03-24-2010, 12:33 PM -
Cant print the contents of a table
By tudorH in forum JDBCReplies: 2Last Post: 03-20-2010, 06:53 PM -
Concatinating contents of a string array based on condition
By gangsterooseven in forum Advanced JavaReplies: 2Last Post: 10-07-2009, 07:35 AM -
Printing the contents of an array of objects
By Mr.Paplu in forum New To JavaReplies: 1Last Post: 03-19-2009, 04:49 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks