![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2005
Posts: 40
Rep Power: 0
![]() |
ArrayLists
Im writing a program to find out if a protein sequence has a turn in it (don't ask) and this is what i want to do.
I need to enter a sequence, at the terminal, i.e. CAENBVWGKL or something then i need to convert this string into an array to separate it all up and give each letter its own place in the array. The rest becomes very complicated biology stuff so i won't bore you with it. I just need to crack this part. ArrayList sequence = new ArrayList //hopefully, eventually a list like this will be made somewhere. sequence[] = ["C","A","E","N" etc.] So if i tried to get sequence[2] i would get "A" back. It seems like it should be pretty straightforward but i can't figure it out. Does anyone know of any good resources where i can see examples of this kind of thing? I feel like i'm going round in circles with google searching for "converting strings to arrayList" and various similar conbinations. Any help? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Do you really need an ArrayList? Is sequence actually going to modified once it has been read? If not, it seems like a waste to use an ArrayList. If all you need is to be able to look up an index in the sequence you can the String class's charAt(index) method on your input string.
|
|
|
|
|
|
#3 |
|
Professional Programmer
|
you could also look into ToCharArray() function that will convert a string into a character array. Wich seems to be exactly what you need.
It doesn't apear like you need an arraylist.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#4 |
|
Programmer
|
You can convert striung to charcters array as suggested by xavier like this
[code] char [] chrArray = strBiologyStuff.ToCharArray(); [\code] Then still you want each chracter in arrayList then you can loop throught this array list using foreach(char chr in chrArray) arrList.Add(chr);
__________________
Iftikhar Ahmed Khan For doing an experiment on programmer's mood please visit http://uxisfyp1.brunel.ac.uk/cspgiak |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Damn that C#, eh? :beard:
On subject, you could either do it with the ArrayList or a char[] as suggested, but if you eventually want to look at substrings as well (e.g. see if ABAB occurred), you might consider keeping it as a string and using charAt(). That way you can use substring() for finding the substrings, rather than selecting a subset of an array[list].
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Dec 2005
Posts: 40
Rep Power: 0
![]() |
Alright thanks for that guys. The reason i was going to use an arraylist was that later on im gonna have to make similar lists for indivdual amino acids which include ints and i thought it would be better to use arraylists which could handle both strings and integers. For example:-
the amino acid Alanine, just A for short would have a list like this. A[142,83,66,0.06,0.076,0.035,0.058] (If you're interested about what those numbers are, the first one is the P(a) which is the propensity of this particular ammino acid to participate in alpha helices. The next is P(b), beta sheets and it goes on like that. I plan to have 20 of these lists, one for each of the most common amino acids which i want to mantipulate to pick out certain values for formulas. Could i just declare each list at the start of the file? ie ArrayList A = new ArrayList ArrayList R = new ArrayList etc.. A[142,83,66,0.06,0.076,0.035,0.058] R[98,93,95,0.07,0.106,0.099,0.085] etc.. then access the different values as i wish with if statements? if(i.equals("A")) {
x = A[1];
}which would hopefully set the x value to 142, to be used in a later calulation. What do you guys think? |
|
|
|
|
|
#8 |
|
Expert Programmer
|
You could use a map to associate each letter with an array of numbers, as demonstrated below. This way there is no need to use if statements to extract values. You might also consider reading the values from a file rather than hard-coding them to make the program more flexible.
HashMap<Character, double[]> map = new HashMap<Character, double[]>();
map.put('A', new double[] { 142,83,66,0.06,0.076,0.035,0.058 });
map.put('R', new double[] { 98,93,95,0.07,0.106,0.099,0.085 });
double x = map.get('A')[0]; // Sets x to the first value in A (142)
double y = map.get('R')[2]; // Sets y to the third value in R (95) |
|
|
|
|
|
#9 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5
![]() |
Another thing you could do is write a class to represent amino acids. It would have methods to get the values associated with a particular amino acid (or you could just expose them as public final constants), an enum definition for the various acids (so that you could not construct an object except for a valid amino acid), and a variable storing the enum value for this particular instance. Assuming you implement the proper interfaces, such as Comparable, you could then create an array of the suckers, and sort, search, etc the array.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#10 |
|
Programmer
|
I think some one already the language I used is C Sharp. Pretty much like Java
. That is a good thing also.
__________________
Iftikhar Ahmed Khan For doing an experiment on programmer's mood please visit http://uxisfyp1.brunel.ac.uk/cspgiak |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|