Is there a way to retrieve a string from an ArrayList if we know the index of the string we want to retrieve? for example in this string:
String text2 = "if(AGE_Y>15){\r\n"
+ "x=PROPERTY_LENGTH;\r\n"
+ "}";
The list this string is added to would look like : if, (, AGE_Y, >, 15, ), {, ...
Now if we loop through the list and add each item to a StringBuilder and define the index for each item:
for (int i = 0; i < list.size(); i++)
{
a=list.get(i);
strBuilder.append(a);
index = strBuilder.length() - a.length();
}
We will know exactly at what index we have the string “PROPERTY_LENGTH” for example.
My question is, how can we retrieve the string “x” from the List? I’m asking because at the line above “x”(or whatever that variable will be named) I want to insert something else in the StringBuilder. Normally we could do something like:
String previous=list.get(indexOfPropertyLength-2);
strBuilder.insert(index-previous.length-1,"string to insert");
Is this at all possible?
Firstly you can rewrite your list iteration to work more efficiently:
To address the title of your question, if you want to simply find a String in a List given an index, you can of course use
myList.get(myIntIndex);. However, it seems that you’re asking how to find an index given a String:myList.indexOf(myString)or something of that nature. If you could be a bit clearer, we might be of more help. Are you writing a hybrid assembler or compiler?