Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList of Characters

I want to create an ArrayList of character objects based off the characters in a string of letters. But, I can't seem to figure out how to fill that ArrayList to match the String. I've been trying to have it iterate through the string so that later, if I change the content of the string it'll adjust. Regardless, no matter what I do I'm getting some sort of syntax error.

Could anybody please guide me in the right direction? Thanks a bunch!

import java.util.ArrayList;
import java.text.CharacterIterator;   //not sure if this is necessary??

public class Test{

//main method

     String words = new String("HELLO GOODBYE!");
     ArrayList<Character> sample = new ArrayList<Character>();

     for(int i = 0; i<words.length(); i++){
         sample.add((char)Character.codePointAt(sample,i));
     }
}
like image 579
user3806226 Avatar asked Jul 13 '14 00:07

user3806226


1 Answers

In your case, words.charAt() is enough.

  import java.util.ArrayList;

  public class Test{

       String words = new String("HELLO GOODBYE!");
       ArrayList<Character> sample = new ArrayList<Character>();

       for(int i = 0; i<words.length(); i++){
           sample.add(words.charAt(i));
       }
  }

Read more: Stirng, Autoboxing

like image 139
Tony Avatar answered Oct 20 '22 01:10

Tony