• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with Array Lists and Loops!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please DoYourOwnHomework . any way ,what do you think about "Stack"? . and welcome to javaranch
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic