Thread: ArrayLists
View Single Post
Old Nov 9th, 2006, 5:52 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I would use Python rather than Java for this sort of task.

In Java, however, you should only use an ArrayList over a char[] array if you will be adding/removing elements after you initially create the collection.

@Iftikhar: You miscapitalized "ToCharArray" in your code (it should be "toCharArray"). As for your enhanced for loop, the keyword is still "for", not "foreach", and the separator is a colon, not "in". Also, the add method for an ArrayList is "add", not "Add". Next time please ensure that your code compiles and runs successfully before posting.

// The original sequence as a String
String sequence = "CAENBVWGKL";

// Convert it to an array of char
char[] seqChars = sequence.toCharArray();

// Now you can easily access each character
System.out.println(seqChars[0]);

// If you want an ArrayList instead
ArrayList<Character> seqList = new ArrayList<Character>();
for (char c : seqChars)
    seqList.add(c); // Add each character to the list
titaniumdecoy is offline   Reply With Quote