• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print strings in a reversed Generic String List individually  RSS feed

 
John Progton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have created a simple program while learning generic. It takes an String [] and places in a Generic List .Everything works fine except the part when I now reverse the list. The List is reversed using the Collections.reverse method but I can't seem to get the individual items to print out on a line instead it prints out the whole array I gather. I suspect somehow i have to use the index value to print out the value
in my for loop but I have tried but I am not succeeding

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I just wanted to make a correction to your code that i'm certain you forgot to write, but its ok. Line 17 should actually be
Well, when i ran the corrected code, i get the following output as follows
[code=java]
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, list contains the elements of the array in this case its 4 elements and then you are using a for loop which loops from index 0 till index 3 and it keeps printing the list elements. According to your requirement you desire individual list elements to be popped out and for that perhaps this link http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html might help you.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Progton wrote:I suspect somehow i have to use the index value to print out the value in my for loop but I have tried but I am not succeeding...

Well, first: Welcome to JavaRanch, and congratulations on asking a nicely framed question that gives us all the information we need to help you. You'd be surprised how many don't.

Next: What do you think:
System.out.println("Reversed "+ list);
(line 28) will do?

Think about it; because I suspect you'll answer your own question right there.

Also: Your loop statement at line 27 contains a mistake (although it'll probably compile). Can you see what it is?

Winston
 
John Progton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Hello,
I just wanted to make a correction to your code that i'm certain you forgot to write, but its ok. Line 17 should actually be
Well, when i ran the corrected code, i get the following output as follows
[code=java]

That output can't be right. There is not even a string called "java". :-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Progton wrote:That output can't be right. There is not even a string called "java". :-)

Typo I suspect. Happens to us all.

However, Ashish is quite right. In version 7, you also have this option:
List<String> list=new ArrayList<>();

Winston
 
John Progton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first: Welcome to JavaRanch, and congratulations on asking a nicely framed question that gives us all the information we need to help you. You'd be surprised how many don't.

Thank you Winston.Welcome to me too.


Think about it; because I suspect you'll answer your own question right there.


I think I know what you are referring to and it what I knew before posting.The " list" prints out the whole Array and it prints it out 4 times but reversed. This is where my pain was . I tried list[i] but that was a no go.I knew soemhow I had to convert the lst back to an array

Also: Your loop statement at line 27 contains a mistake (although it'll probably compile). Can you see what it is?
It compiles but you say there is an error ...*Clenches teeth* ...Umm colors.length perhaps but that gave an error.So I am still fuzzy as to why the loop as was is incorrect even though I finally got a solution working

Finally!*Gives himself pat on the back*



The solution works fine but is there a better more effecient way or is this it? I want to learn good habits from the start.
And thanks you made me scratch my brain more.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Progton wrote:The solution works fine but is there a better more effecient way or is this it?

Answer (as it almost always is, even for us old farts): yes.

1. You don't need to convert back to an array, you simply need to find the method in List that returns you a single element at a specified index (ie, List's equivalent of array[i]). It's there, just look at the docs for java.util.List (←click).

2. The problem with your original loop was that it uses colors.length after you just went to all that trouble to create a List. It'll actually work for what you were doing, but if you ever decided to remove all the 'greens', for example, before printing it out, it wouldn't. If you want to be sure that it works, you need to use the List's length to control the loop.

HIH

Winston
 
John Progton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
John Progton wrote:The solution works fine but is there a better more effecient way or is this it?

Answer (as it almost always is, even for us old farts): yes.

1. You don't need to convert back to an array, you simply need to find the method in List that returns you a single element at a specified index (ie, List's equivalent of array[i]). It's there, just look at the docs for java.util.List (←click).

2. The problem with your original loop was that it uses colors.length after you just went to all that trouble to create a List. It'll actually work for what you were doing, but if you ever decided to remove all the 'greens', for example, before printing it out, it wouldn't. If you want to be sure that it works, you need to use the List's length to control the loop.

HIH

Winston


Thanks so much would this then be the better solution.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:I just wanted to make a correction to your code that i'm certain you forgot to write, but its ok. Line 17 should actually be

No, the code that John wrote is perfectly valid Java 7 code, no correction is needed:

See: The Diamond.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Progton wrote:Thanks so much would this then be the better solution.

Aha! I do believe you've got it, Watson. Time for a celebratory cup of tea. Mrs. Hudson!...

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!