I want to store city names by country in an array.
This is my code
String cities[][]=new String[10][20];
I want to assign all cities of a country one time like this.
cities["USA"]={"NEW YORK","WASHINGTON"}
cities["UK"]={"LONDON","CAMBRIDGE","CARLISLE"}
then I want to use like this
String mycity=cities["UK"][2];
but eclipse shows error for assigning values. how can I use this arrays?
Try like this,
String cities[][]={
{"NEW YORK","WASHINGTON"},
{"LONDON","CAMBRIDGE","CARLISLE"}
};
And
cities[0][0]
will return NEW YORK
May be this helps you.
Better you can use a HashMap - List combination like this
HashMap<String,HashMap<String,List<String>>> cities = new HashMap<>();
Refer following links for more details Storing HashMap inside HashMap,
Storing a HashMap inside another HashMap and improving performance
Related
Currently I have this object:
CurrencyType(Stuff1=null, Stuff2=0.61952, Stuff3=1.2117, Stuff4=2.9687, Stuff51=0.95025, Stuf6=0.69852, Stuff7=4.9222, Stuff8=15.931, Stuff9=4.6196, Stuff10=0.55648, Stuff11=5.6577, Stuff12=4.6056, Stuff13=202.27, Stuff14=10732.0, Stuff15=2.5838, Stuff16=51.865, Stuff17=79.175, Stuff18=80.258, Stuff19=808.33, Stuff20=13.856)
Since I want to display this results in a Recycler View and the JSON does not give me an Array I need to split these values into an Array. Can someone show me how to do it?
maybe you can use reflection like in these answers:
Object Attributes in Java as Array or List Elements
Getting a list of all fields in an object
As they say in the answers, if you could avoid using reflection and try to have your array directly, don't hesitate (reflection raises performance issues).
Convert Object to String(.toString())
String datam="CurrencyType(Stuff1=null, Stuff2=0.61952, Stuff3=1.2117, Stuff4=2.9687, Stuff51=0.95025, Stuf6=0.69852, Stuff7=4.9222, Stuff8=15.931, Stuff9=4.6196, Stuff10=0.55648, Stuff11=5.6577, Stuff12=4.6056, Stuff13=202.27, Stuff14=10732.0, Stuff15=2.5838, Stuff16=51.865, Stuff17=79.175, Stuff18=80.258, Stuff19=808.33, Stuff20=13.856)";
String[] arr_one=datam.replace("CurrencyType(","").replace(")","").split(",");
List<String> stringList = new ArrayList<String>(Arrays.asList(arr_one));
This is not perfect answer but it may help you
I have a list of objects that I needed to sort by category and also alphabetically. My idea was to take my ArrayList, convert it to a HashMap so that I could use a key to organize my data by category, and then createe a treemap to naturally alphabetize the data. It works, and I am not certain of a better/more efficient way of doing this. My TreeMap has the following structure:
TreeMap<String, ArrayList<CustomModel>>
How do I access the list of values from this treemap? I understand that I can get a specific value, if it were a String for example like the following:
for (Map.Entry<String, String> entry : treemap.entrySet()) {
Log.i(TAG, "key= " + entry.getKey());
Log.i(TAG, "value= " + entry.getValue());
}
However, what if you have a list of values? How do I retrieve everything within that list? Seems like I need another conversion.
It seems like you already have this solved. Entry.value() will return the ArrayList, instead of the String type. Try enumerating this and then logging each value
I got the answer. There are a couple of ways of doing this. You can retrieve an object from your list, or the entire list object.
1) Object From List
You need a key to do this option. Get the key using keySet()
Object key = treemap.keySet().toArray()[index];
Object value = treemap.get(key);
2) Entire List Object
ArrayList<CustomModel> alCustomModel = (ArrayList<CustomModel>) treemap.values().toArray()[index];
Some notes about this, is that you are retrieving the list of CustomModels per position. Use whichever methodology makes sense to you. Cheers!
I have added all lat/lng to arraylist and .Iwould like to map it to string name and another string along with its id which is an int .Basically I would like to get :
Association : (arraylist)-->Name---->Another Name------>id
how do I do the above association.I am a noob in android and I am using hashmap but it only puts(key,value) which does not satisfy the above condition.
Please let me know how I can implement the above requirement.
I appreciate any help.
Thanks in Advance.
There are a number of ways you could accomplish this. Here are two different ways off the top of my head:
Create an object that will hold the two names and id. Then create a hashmap that maps each lat/long value to the appropriate object. The prototype would be like:
HashMap<Long/Lat, Object>
Or if you don't want to create a new object, then create a hashmap that maps each lat/long value to a hashmap that contains the two names and id. The prototype would be like:
HashMap<Long/Lat, HashMap<String, String>>
I want to group all contact in my app by using the first character of contact name. The result looks like deafault Contact Book in Android Phone.
I don't know the name of API which can solve my problem.
Could you give me the name of it?
Picture figures: image
You can use the an object of TreeSet class to sort strings as shown below:
TreeSet mySet = new TreeSet();
mySet.add("java");
mySet.add("C");
mySet.add("Pascal");
mySet.add("ruby");
Log.d("TAG",mySet);//output here will be C,java,Pascal,ruby,
//Now our task is to fetch strings from the sorted strings in `mySet` object
String[] names= mySet.toArray(new String[mySet.size()]);
//Now you will have sorted names in the names[] array
So, as commented if you are able to fetch string/names from your contacts, then you can use the above concept by creating an object of type TreeSet and adding all your contacts to this object and converting it to an array of strings as shown above.
I'm sure this one definitely helps.
I created a list based on this example:
http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
In GetView method, need to access information that is stored only in the HashMap.
I can access using "adapter.getItem (position). ToString ()" but this way, keys and values come together in a single string, like this:
{date=2011-07-25 19:30:00, id=1, caption=Test Caption, title=Test Title, bookmark=true}
You can retrieve this data separately? For example, only the value of "bookmark" field
Please include an example. I am new to developing for Android.
remember that you know the data type that is used with the adapter.
instead of using toString, just get the item and use it as you usually do.
for example:
HashMap<String, String> i = (HashMap<String, String>) adapter.getItem(position);
i.get(ITEM_TITLE);