File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with Array Lists and Loops! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Array Lists and Loops!" Watch "Help with Array Lists and Loops!" New topic
Author

Help with Array Lists and Loops!

Kay Jay
Greenhorn

Joined: Nov 16, 2009
Posts: 2
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

Joined: May 28, 2009
Posts: 17
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

Joined: Jan 28, 2008
Posts: 5575

Please DoYourOwnHomework . any way ,what do you think about "Stack"? . and welcome to javaranch
suresh senapathi
Greenhorn

Joined: Jan 14, 2009
Posts: 13
The following may helps for your requirement

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

Regards,
Suresh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Jan 14, 2009
Posts: 13
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
suresh senapathi
Greenhorn

Joined: Jan 14, 2009
Posts: 13
Hi,
Thanks for your help. I will improve my self by following thing what you are given me.

Regards,
Suresh
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 13, 2005
Posts: 39409
    
  28
Only a minor point. We all do that sort of thing
suresh senapathi
Greenhorn

Joined: Jan 14, 2009
Posts: 13
HI,

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

 
Don't get me started about those stupid light bulbs.
 
subject: Help with Array Lists and Loops!