Loading a large of amounts of strings into my ArrayList, I don't want to clog up my class?

If you have the values saved in strings. Xml file you can simply do this: ArrayList values = new ArrayList(); Collections. AddAll(values, getResources.

GetStringArray(R.array. Words)) or List values = Arrays. AsList(getResources.

GetStringArray(R.array. Words)).

If you have the values saved in strings. Xml file you can simply do this: ArrayList values = new ArrayList(); Collections. AddAll(values, getResources.

GetStringArray(R.array. Words)); or List values = Arrays. AsList(getResources.

GetStringArray(R.array. Words)).

1, very Android-ish.... – st0le Nov 27 '10 at 16:49.

Here is an example of reading the "words. Txt"file from the assets-direcytory in your project. Each word on a line by itself.

/* * If you know the number of words at compile time, * specify it here in the initial capacity */ ArrayList words = new ArrayList(50); try { InputStream is = getResources().getAssets(). Open("words. Txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while((line = br.readLine())!

= null) { words. Add(line); } is.close(); } catch (IOException e) { e.printStackTrace(); }.

Clearly the best choice... – st0le Nov 27 '10 at 16:49.

Put the 100's of strings into a file and read from it. Iterate using a for loop to add these strings in. If you do not want to use a file, user String words = new String{"test","me"..."last word"} and then iterate over it.

If my strings are in a StringArray (in my strings. Xml) and I create an ArrayList, I can run the StringArray through a For Each loop and be good right? – Noah Nov 27 '10 at 8:42.

If you have thousands of words, probably putting them into a txt file is a good solution. However, if the number of words are not that much you can do one more thing to avoid calling add() multiple times: String myArray = {"stack", "oveflow", "java", "array",...}; //large array with lot of Strings. List list = Arrays.

AsList(myArray).

If the xml file is in some specific format you can also easily read them all using dom4j for example: you can read with this code: String xmlFileName = "D:/validation/validator/src/summa/sample. Xml"; String xPath = "//Root/Address"; Document document = getDocument( xmlFileName ); List nodes = document. SelectNodes( xPath ); for (Node node : nodes) { String studentId = node.

ValueOf( "@studentId" ); stringArray. Add( studentId ); }.

Adding a library for a trivial task, bit too much... – st0le Nov 27 '10 at 16:49 if that is the only use, I agree... but usually when you start with xml you'll finish to use it in a lot of places. Moreover dom4j is quite lightweight... – Uberto Nov 27 '10 at 18:08.

I have a ArrayList and I want to put a lot of strings into it (several hundred) but I don't want to code around a huge list of .add()'s and such. Can I important the strings from my strings. Xml file into the ArrayList?

If so, how?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions