I am currently creating an app that fetches 2 Strings randomly from an arraylist.
This is what I did:
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("someString");
// continue arrayList.add() for another thousand times for different strings.
// I am adding the Strings manually as these are specific questions in a quiz app
Random random = new Random();
myTextView.setText(arrayList.get(random.nextInt(arrayList.size());
myTextView2.setText(arrayList.get(random.nextInt(arrayList.size());
The Question:
At any point in time, I only need 2 Strings to be displayed to the user. But I am populating a thousand element arrayList, and fetching only 2 Strings from there. Is there a better way to do this? For example, only populating 2 Strings at runtime, without populating the unnecessary Strings?
Or maybe it does not matter at all?
Thank you very much in advance!
Related
I have an unsorted arraylist with 70000 string values. I want to add same values in separate list.
i.e
if the sample of unsorted list is like this
Arraylist[0]->"NewYork"
Arraylist[1]->"DC"
....
....
....
Arraylist[401]->"NewYork"
Arraylist[402]->"Seoul"
Arraylist[403]->"DC"
if 2 or more same values are found from the unsorted list, i want to add in separate list or multihashmap which can store same values, as i want to create sections for same values. Result would be like this
Section1:
Arraylist1.add("NewYork");
Arraylist1.add("NewYork");
Section2:
Arraylist2.add("DC");
Arraylist2.add("DC");
In my perspective as unsorted list can be random , Creating multiple arraylist is bad approach instead i have used like multihashmap for sections.
The thing is i do not want code because i already implemented it,the thing is above scenario for finding strings and sorting them in each section but my algorithm is way slow, it takes about 30 to 40 seconds, my issue is which is the fastest way to perform this so i can do this in lesser and minimal time.
I'm making a quiz app I have string array and I want to load them on button click, strings should be loaded randomly into Text View. Strings should not be repeated.
Thanks..
Because it's your quiz, I'm gonna give you hints not the whole soltion
Random rnd = new Random;
while(array.size()>0){
// 1. use rng to get a index between 0 to current array.size()
// 2. remove the string by array.remove(index) so that it won't duplicated.
// 3. setText(string)
}
Try this, you can add comments if you need more details.
One way to do it is to convert the array to a list and shuffling it.
List myList = Arrays.asList(yourArray);
Collections.shuffle(myList);
After that you can just iterate over the list.
so this must have been asked before, but i could'nt find something useful, so sry.
Here is my scenario:
I want to store Information about Items, their price and other values in an ArrayList. Lets say we have about 10.000 items in the list when we're done. My question is, what way is the fastet / what takes less resources for the system so that the app runs most efficient.
Should i:
use one ArrayList for all the Data Like this
ArrayList<String> items = new ArrayList<String>();
items.add("name;price;from");
String[] seperated = items.get(i).split(";");
seperated[0]...
and then have to split it whenever i need to read from it.
(will be each time a customer is searching an item, i could then only split the entries that fit the search criteria)
or should i use 3 ArrayLists with 10.000 Items each.
Thanks in advance :)
Faster parsing - multiple arrays.
Less memory used - single array.
It depends on how large you expect the array(s) to become. A proper way would be to load only as many items as are needed. E.g. if on the screen you see 10 items, you load 30 and (un)load more as they are removed/requested.
I'm not 100% clear on your spec, but if it were me, I would implement an Item object:
class Item {
private String _id;
private String _name;
private String _price;
private String _from;
//getters,setters,etc.
}
Then have a HashMap to look up the Items by id.
HashMap<String,Item> items = new HashMap<>();
You can get a list of all your items via:
items.values();
Or, you can perform an O(1) lookup via id:
items.get(someId);
I have a small issue with ArrayList. I have to fetch the document from the server.
The document contains 7 fields of data. I have to show the document names in the list view.
For this I have added different fields data to the different ArrayList. So when I click on the document name, based on the position of the document, I fetched the all fields data from the different Arraylist based on the position.
But Have a small issue using by using the above procedure. Is there any procedure that is not depend on the position, what I want is irrespective of position if I click on the Document,based on the keyword document data to be extract.
Any Help Appreciated.
Thanks in Advance.
I got your point. If you try to manage different ArrayLists then it would be difficult to manage it. I mean if you delete item from particular position from particular ArrayList then you will have to delete items from same position from other ArrayList, if you forgot to do so then it will be unbalanced.
Solution:
Instead feasible solution is to create ArrayList<Object> or ArrayList<HashMap<String,String>>, so your every item is the type of particular object, and every object contains detail and everything of particular items.
For example: ArrayList<Documents>, here ArrayList will contains list of Documents objects, and every objects contains values of 7 fields.
Its simply easy to define Documents class with getter/setter attributes.
I am developing an application which involves a quiz in it. I have used a viewflipper for displaying question, as i press the next button it flips to next question. However, I wish to display these questions randomly. I have parsed an xml and displayed the questions n its options !
Assuming that all your questions stored in array or some data structure e.g. ArrayList
You need to define a single random object only once in your activity
e.g.
Random random = new Random();
Get the next question index:
int nextQuestionIndex = random.nextInt(n); // where n is the total number of questions