Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Array Lists and Loops!

 
Kay Jay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys - this is officially my first post on this site, so I'm hoping it works out well for me. I am spanking new to Java, and am currently working on a homework assignment and I'm caught up on these loops and arrays. The question seems simple, I'm just not starting off too well. The second loop is what confuses me most. Here's the question:

Write a loop that reads 10 strings and inserts them into an array list. Write a second loop that prints out the strings in the opposite order from which they were entered.


Here's what I have so far. And be easy on me!

ArrayList <String> read = new ArrayList <String>();
{
String[] read = new String[10];
for (String i = 0; i < read, i++)
{
System.out.println(read[i]);
}


What in sam goodies am I doing wrong and where do I start with fixing this mess. LOL! Thanks so much everyone.
 
Victor Ewert
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, you have declared the variable read, as both an ArrayList object and an Array. From what the assignment says, you should be using ArrayList so just keep that one.

The assignment doesn't say how you are supposed to 'read' the Strings. Are they supposed to be input from the console, read from a file, or ???

Basically what you need to read in your Strings is



Then look at the Collections class to reverse the order

Next, to output the reversed Strings, a loop like the following should work:



Read up the Java API docs (http://java.sun.com/javase/6/docs/api/) on ArrayList, Iterator and Collections classes.

Hope this gets you started in the right direction.

Victor Ewert
Ewert Technologies
http://www.ewert-technologies.ca
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please DoYourOwnHomework . any way ,what do you think about "Stack"? . and welcome to javaranch
 
suresh senapathi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following may helps for your requirement

[edit]I didn't think it helps. See next post. CR[/edit]

Regards,
Suresh
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:Please DoYourOwnHomework . any way ,what do you think about "Stack"? . and welcome to javaranch
I think Victor Ewert's answer was at the correct level; he explained what is going wrong, and also corrected Kay Jay's interpretation of the question.
It says to use ArrayList, so you have to forget about Stacks.
But suresh senapathi's answer does not help. This, I think, has overstepped the mark between helping and doing somebody else's homework. Apart from some poor design features, giving answers like that does not help at all, so I shall feel obliged to "pull rank" and delete the answer.
 
suresh senapathi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be Victor Ewert answer is correct.BUt in that he diplayed what ever the data exist in array list.but seetharaman venkatasamy asks data in reverse order from the list.

Q: Write a loop that reads 10 strings and inserts them into an array list. Write a second loop that prints out the strings in the opposite order from which they were entered. That's why i have give my understanding.i am not sure it is correct.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kay Jay wrote:

Welcome to the Ranch.

A couple of things for you to do.
Please check your private messages for an important administrative matter.
Also please check out these tips given here http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch to get the most of the Ranch.
 
suresh senapathi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your help. I will improve my self by following thing what you are given me.

Regards,
Suresh
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
seetharaman venkatasamy wrote:Please DoYourOwnHomework . any way ,what do you think about "Stack"? . and welcome to javaranch

It says to use ArrayList, so you have to forget about Stacks.

Yes,thanks. Sorry for my bad concentration
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only a minor point. We all do that sort of thing
 
suresh senapathi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

It' ok. That's not a problem.
BUt your questions are intresting.
Ok keep it up all the best for your future.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic