-
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.
-
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!
-
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?
Quote:
Originally Posted by
forms
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.
-
Re: i want to print the contents of the array, can you help?
Thanks so much for your help it worked!
-
Re: i want to print the contents of the array, can you help?
-
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!
-
Re: i want to print the contents of the array, can you help?
you can use Iterator to iterate through the list.....
-
Re: i want to print the contents of the array, can you help?
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...
-
Re: i want to print the contents of the array, can you help?
Yes, since your lastItem is initialized to -1, this loop:
Code:
for(i=lastItem; i>=location; i--){
a[i+1]=a[i];
}
doesn't run. It goes straight to
Code:
a[location]=newItem;
-
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
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] + " ");
}
}
}
Code:
public class UseList {
public static void main(String[] args){
List l = new List();
l.insertItem(4,1);
l.print();
}
}
-
Re: i want to print the contents of the array, can you help?
Quote:
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.
-
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?
-
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.
-
Re: i want to print the contents of the array, can you help?
Quote:
Originally Posted by
pbrockway2
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.
-
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":
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.
-
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?