A few things I want to mention here about the boards. Please put all your code in code tags (that is the pound symbol on the editor). This makes it easier for us to look at your code. Secondly, in the future please show the entire method here so that we can see all the lines you are using. If this is all you had, then I suggest you come with perhaps a more completed "attempt". Third, always state your problem clearly and if possible in the form of a question which we can help you answer. Oh and in the future, NEVER use a dollar sign as a variable name. That is so wrong programming style that other programmers will take your head off if they see that in production code.
Ok... so I am going to assume you are attempting to take in a string, reverse the characters using a Character arrayList implementation.
Here you go. Please read the comments to see what I am doing and how I am doing things.
CODE
import java.util.*;
public class reverse {
public static void main(String args[]) {
// Setup our Character array (notice this is the reference type of Character here)
List <Character> ListOfNames = new ArrayList <Character>(10);
// Prompt the user for a string value and store it.
Scanner inputScanner = new Scanner(System.in);
System.out.print("Enter a string value: ");
String line = inputScanner.nextLine();
// Show the user what we have input
System.out.println("The Inputted String: " + line);
// Loop through the characters to show it in order
for(int i = 0; i< line.length(); i++)
{
// Take each character and through "boxing" we box it up in a Character object.
System.out.println("Character is: " + line.charAt(i));
ListOfNames.add(line.charAt(i));
}
System.out.println();
// Reverse the character elements
Collections.reverse(ListOfNames);
// Show characters again to see them in reverse.
System.out.println("Now to show the reversed version...");
for (Character c : ListOfNames) {
System.out.println("Character is: " + c.toString());
}
}
}
Notice how I first store each character into the array by taking each char and boxing it up into a
Character object. This is called "auto boxing". So now we have a list of Character objects in our arrayList. I go ahead and print them so you can see that they are stored in order. Next I reverse them using the static method reverse in our collections object. I then go back and reprint the list to show you that the characters are now in reverse order.
Enjoy!
"At DIC we be coding ninjas!"
This post has been edited by Martyr2: 12 Oct, 2007 - 09:19 AM