Java Printing Array String&index No
- McPaper
-
McPaper
- Member since: Nov. 29, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,162)
- Block
-
- Forum Stats
- Member
- Level 21
- Programmer
Im still learning object oriented java so excuse me if what I am asking is obvious.
An excercise in the book I am working from wants me to make a method which prints out all the bits of data held in a arraylist of type string along side their index number.
I can manage to print out the String contents of one of the array values however I am unsure how to incorperate its index number.
I understand that I am meant to use the method .size with type int however I am clueless how I impliment it as another local variable within the for loop.
I figure if that is possible ill just stick localvarindex + ": " + localvar within the println.
public void listAllNotes(){
for(String localvar : notes){
System.out.println(localvar);}
} - Montycarlo
-
Montycarlo
- Member since: Mar. 14, 2005
- Offline.
-
- Forum Stats
- Member
- Level 24
- Blank Slate
for(int x=0;x<string.length();x++) System.out.println("#" + x + " -> " + x.charAt(x)) Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
At 12/24/09 10:30 PM, McPaper wrote: Im still learning object oriented java so excuse me if what I am asking is obvious.
It is not obvious. You have also used the wrong type of for loop.
You need the "basic" for loop. You are using a for "each" loop. It's great shorthand, but sketchy on the details.
I can manage to print out the String contents of one of the array values however I am unsure how to incorperate its index number.
If you use the basic for loop, the index is easily at hand. You can make that for each loop work, But using that is like hammering a nail with the butt end of hammer. Possible but very very wrong.
"I understand that I am meant to use the method .size with type int however I am clueless how I impliment it as another local variable within the for loop."
.size .length .getlength
Whatever, this won't change. When index>length in a basic for loop, the loop ends.
12/24/09 10:30 PM, McPaper wrote:
:I am clueless how I impliment it as another local variable within the for loop.
I will not teach you to write such horrible code.
Do you know what a while loop is?
- McPaper
-
McPaper
- Member since: Nov. 29, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,162)
- Block
-
- Forum Stats
- Member
- Level 21
- Programmer
Do you know what a while loop is?
My book covers that later but I think the only difference is that its based on a condition where as for-each is on looping the body for each x in y? (You could probably clarify this better)
What I am having difficulty with the for-each loop is expressing the instructions in the paranthesis. I dont really understand why I put in a localvariable also.
But what you are saying is that I can achieve what I want (printing all array strings with their associated index number) with a basic loop?
Are the types of loops defined by the content of their associated parenthesis?
- McPaper
-
McPaper
- Member since: Nov. 29, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,162)
- Block
-
- Forum Stats
- Member
- Level 21
- Programmer
Well Ive done something right and managed to output:
0: ssssssssss
1: blah
2: dasdsa
3: 44xdsa
with:
public void listAllNotes(){
int y = notes.size();
for(int x=0, w=0; x < y;){
System.out.println(x + ": " + notes.get(w));
x++;
w++;
}
} - kiwi-kiwi
-
kiwi-kiwi
- Member since: Mar. 6, 2009
- Offline.
-
- Forum Stats
- Member
- Level 09
- Programmer
At 12/26/09 01:21 PM, McPaper wrote: with:
public void listAllNotes(){
int y = notes.size();
for(int x=0, w=0; x < y;){
System.out.println(x + ": " + notes.get(w));
x++;
w++;
}
}
First of all notice that w==x and therefore you don't actually need it
Also I guess I can understand why you'd want to increment x inside the loop rather than using all the statements of the for loop, but generally the other variant. Normally I separate the increment from the for(...) when it's some weird formula because I don't want to clutter the code
- McPaper
-
McPaper
- Member since: Nov. 29, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,162)
- Block
-
- Forum Stats
- Member
- Level 21
- Programmer
At 12/26/09 02:46 PM, kiwi-kiwi wrote: First of all notice that w==x and therefore you don't actually need it
You are right, its funny how things can slip you.
Also I guess I can understand why you'd want to increment x inside the loop rather than using all the statements of the for loop.
Yes I initially wanted to put the increment inside the loop header but it wouldnt let me put it with an additional w++ in the header. So I put w++ in the body and stuck x++ there too for consistency.
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
You posted this code (I removed the w)
public void listAllNotes(){
int y = notes.size();
for(int x=0; x < y;){
System.out.println(x + ": " + notes.get(x));
x++;
}
}
Usually, the x++ is inside the for(;;).
This code is identical to the code above.
public void listAllNotes(){
int y = notes.size();
for(int x=0; x < y; x++){
System.out.println(x + ": " + notes.get(x));
}
}
As well, both of those are short hand for a while loop.
public void listAllNotes(){
int y = notes.size();
int x=0;
do {
System.out.println(x + ": " + notes.get(x));
x++
} while (x < y;);
}
// if(x<y) for any of those loops, the loop exits. You can do this by hand with "break;" if you need to exit in the middle.
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
for(int x=0;x<string.length();x++) System.out.println("#" + x + " -> " + x.charAt(x));
Equals
for(int x=0;x<string.length();) {System.out.println("#" + x + " -> " + x.charAt(x)); x++;}
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
- gumOnShoe
-
gumOnShoe
- Member since: May. 29, 2004
- Offline.
-
- Send Private Message
- Browse All Posts (15,244)
- Block
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 12/24/09 10:30 PM, McPaper wrote: public void listAllNotes(){
for(String localvar : notes){
System.out.println(localvar);}
}
If you wanted to do it this way, with this loop, you'd merely have to do the following:
public void listAllNotes() {
int x = 0;
for(String localvar : notes) {
System.out.println("[" + (x++) + "] : " + localvar;
}
}
As already pointed out, the other variation of the for loop is more appropriate, but not necessary.
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
At 12/29/09 11:33 AM, gumOnShoe wrote:At 12/24/09 10:30 PM, McPaper wrote: public void listAllNotes(){If you wanted to do it this way, with this loop, you'd merely have to do the following:
for(String localvar : notes){
System.out.println(localvar);}
}
public void listAllNotes() {
int x = 0;
for(String localvar : notes) {
System.out.println("[" + (x++) + "] : " + localvar;
}
}
As already pointed out, the other variation of the for loop is more appropriate, but not necessary.
The problem is that, as an exercise from the book, you are meant to learn how to make a for loop. If you don't know the true form of a for loop you could encounter impossible problems later in the class. I doubt the book taught that shorthand but I could be wrong.
- Sterance
-
Sterance
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 23
- Programmer
I'd just like to toss in my 2 cents on the for-each loop. I really only find it useful when i want to iterate through every element in an array and do something to each element on the spot. I can remember using this a lot when updating the position of an array of bullet objects. The beauty of using a for-each here is you don't need to know how many bullets are on screen, so you don't need to keep track of that number.
When i started school for-each loops weren't even part of java yet, so that should be an indicator of their importance. It seems to be like the ?: operator dealy (which i'm sure you'll learn eventually if you haven't already); it has its uses. mostly i think you'll get the most mileage out of good ol' for loops. not to mention, not every language will have a for-each loop available for use. why did i type so much about that...
Fear Popo Bawa!!!
John Goodman | ¥
while(<>){chomp;print;}
- macareno
-
macareno
- Member since: Sep. 18, 2009
- Offline.
-
- Forum Stats
- Member
- Level 01
- Blank Slate
dude D:
I've using the normal for loop a lot when helping some friends, and one day I came a across with pseudo-code that really made it all clearer for me (and them).
for(<i>declarationOfIterationVar</i>;<i>condition</i>;<i>actionToRepeat</i>){
//code
}
the actionToRepeat part is very interesting in the way that it can be any action, may it be a method, or a simple operation, like incrementing your counter (
x++
)
hope it helps c:
but yeah, when you need to keep track of somethings index it's easier to use the traditional for loop.
- macareno
-
macareno
- Member since: Sep. 18, 2009
- Offline.
-
- Forum Stats
- Member
- Level 01
- Blank Slate
for(declarationOfIterationVar ; condition ; actionToRepeat){
//code
}
sorry :c the italic tags ruined it :c



