How to use a Vector over ArrayList? - android

I am using
String [] = {//Variables"}
To create an ArrayList.
I have been made aware the a Vector collection would be alot better.
If anyone know a tutorial, or wants to post an example of a Vector the give specific detail please help me out.
Thanks Alot.

You can use Vector's copyInto(String[]) to build a vector from string array.
Hope this helps.

A Vector is not usually a better choice because it is synchronized and has performance costs.
Also, your code does not create an ArrayList. It is creating an array of Strings which is quite different.
What you really want to do is:
String [] myArray = {/*Variables*};
List<String> myList = Arrays.asList(myArray);

Related

Hot to convert filled object into an Array of Strings

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

What is the benefit of turning a String array into an ArrayList before populating a AutoCompleteTextView with it?

On the material page I found the following example for AutoCompleteTextView:
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
String[] dogArr = getResources().getStringArray(R.array.dogs_list);
List<String> dogList = Arrays.asList(dogsArr);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, layoutItemId, dogList);
AutoCompleteTextView autocompleteView =
(AutoCompleteTextView) findViewById(R.id.autocompleteView);
autocompleteView.setAdapter(adapter);
Source: https://materialdoc.com/components/autocomplete/
What is the point of this part:
List<String> dogList = Arrays.asList(dogsArr);
Why turning it into an ArrayList when the AutoCompleteTextView also takes a String array?
When you know only going to work with a fixed number of elements, you should Array. If not, use Lists.
My personal opinion is use list. Lists makes code very inflexible and easy to use.
You can initialize Java arrays at compile time, like:
String data[] = { "a", "b", "c" };
In old versions of Java there was also the case for type safety. ArrayList elements had to be casted to the original type whereas Java arrays where type safe.
Java arrays are part of the language and you will not be able to change them. ArrayList is part of the Java API. If you need (I do not recommend it though) you could substitute your own library to implement the ArrayList job
Check This Question For More Information
See This question
If you have an array, it has to have a fixed size. Dynamically adding and removing the elements are difficult to manage and you have to have new array created every time you add a new item. Similarly for removing item.
With ArrayList it is easy to manage as it doesn't get created with a static size. Thus at runtime you can easily add and remove elements.
ArrayList is the ideal datastructure to use here.

output text line by line

I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work

give each place in array randomaly button-android

I need some idea of how randomaly give each button[i] on of the values R.id.buttonj_mg.
(one to one function...).
I don't know how to do it since R.id.button1_mg is not a string, so I can't do somethink like R.id.button+j+_mg when j chossen randomaly..
This is the situation now:
button[1]= (Button)findViewById(R.id.button1_mg);
button[2]= (Button)findViewById(R.id.button2_mg);
button[3]= (Button)findViewById(R.id.button3_mg);
button[4]= (Button)findViewById(R.id.button4_mg);
button[5]= (Button)findViewById(R.id.button5_mg);
button[6]= (Button)findViewById(R.id.button6_mg);
button[7]= (Button)findViewById(R.id.button7_mg);
button[8]= (Button)findViewById(R.id.button8_mg);
button[9]= (Button)findViewById(R.id.button9_mg);
button[10]= (Button)findViewById(R.id.button10_mg);
button[11]= (Button)findViewById(R.id.button11_mg);
button[12]= (Button)findViewById(R.id.button12_mg);
button[13]= (Button)findViewById(R.id.button13_mg);
button[14]= (Button)findViewById(R.id.button14_mg);
button[15]= (Button)findViewById(R.id.button15_mg);
button[16]= (Button)findViewById(R.id.button16_mg);
You could use a collection to store your ints as Integers and then use the Java Collection class shuffle() method on those objects. Then you could remove them one by one from the Collection in each one of your buttons.
List<Integer> resources = new ArrayList<Integer>();
...
resources.add(R.id.button1);
...
Collections.shuffle(resources);
One solution is to create the buttons and their ids in the code instead of taking them from resources, look at https://stackoverflow.com/a/11615356/987358. Then you can store them easily in a collection as another answer suggests.
Another solution is the Java reflection API which allows to retrieve the values of the ids using strings of the id names.

Resource issue, (R.drawable.*VARIABLE HERE*); possible?! ANDROID

I'm having an arrayList with loads of numbers, I want
to have a different image on every number in the array represented by each number... but I dont know how to put a Variable in (R.drawable."HERE"), if its even possible?!
This is what i've tried to do, Im pretty new to android and Java,
so this may seem pretty funny to you haha... (the array is not shown)
String s = names[position];
String img = s.toString();
holder.imageView.setImageResource(R.drawable.img);
Hope you understand my question.
Thanks in advance
//Halle
You can use reflection do do that. See here for an example.

Categories

Resources