adding objects to a ArrayList<String> - android

I believe you can add objects use list.add(); However, is there an alternative way of making an ArrayList of strings? Something like ArrayList("heyey","hgfhgfh","fhfghgf") ?

List<String> listString = Arrays.asList(new String[] {"heyey","hgfhgfh","fhfghgf"});
With listString being a fixed size list (see Arrays.asList).
If you need a variable size list:
List<String> listString = new ArrayList<String<(Arrays.asList(new String[] {"heyey","hgfhgfh","fhfghgf"}));

List<String> strings = Arrays.asList("ohai", "I", "have", "varargs");
Or if you're kickin' it old school:
List strings = new ArrayList() {{
add("ohai");
add("Java 1.4");
add("lives");
}};

Related

Split strings on string array

i have String Array like this:
String[] q1={"AAA-BBB","AAA-CCC","AAA-DDD"}
and i want result like this
temp={"BBB","CCC","DDD"}
i tried below code but the result is wrong
for(int i=0;i<q1.length;i++){
ArrayList<String> temp=new ArrayList<>(Arrays.asList(q1[i].split("AAA-")));
}
Try like this:
ArrayList<String> temp=new ArrayList<>();
for(int i=0;i<q1.length;i++){
String[] array = q1[i].split("-");
temp.add(array[1]);
}
You could use substring:
ArrayList<String> temp = new ArrayList<>();
for(int i=0; i<q1.length; i++){
temp.add(q[i].substring(q[i].indexOf('-') + 1, q[i].length()))
}
you find error Because you use split
Splits this string around matches of the given regular expression.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
q1[i].split("AAA-")
in this line you got 2 result splited 0 = "" AND 1 = "BBB"
so you need to pick the sec result
you have multi Solution
like https://stackoverflow.com/a/50234408/6998825 said
String[] array = q1[i].split("-");
temp.add(array[1]);
//change this q1[i].split("AAA-") to
q1[0].substring(4)
if your AAA- is not going to change
Have you tried creating the ArrayList outside of the loop? As previously you were creating a new ArrayList for every element in your string array
ArrayList<String> temp = new ArrayList<>();
for(int i=0;i<q1.length;i++){
temp.add(q1[i].substring(4);
}
Assuming that "AAA-" is not going to change.

Array list wont display in my listview Android

Please someone help me how to show all list array in listview, i have "eventname" which values are: "Party", "Study", "Exam".
and inside my for loop i have this codes and it only outputs "Exam"..
lv = (ListView) findViewById(R.id.textView);
List<String> your_array_list = new ArrayList<String>();
your_array_list.add(eventname);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(CalendarActivity.this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
You are not adding data in your arraylist correctly..you are adding only one data in arraylist.
String[] array ={"EXAM","Study"};
for(String str: array){
your_array_list.add(str);
}
You code works properly, you are adding just one item:
your_array_list.add(eventname);
If you want to add the items "Party", "Study", "Exam", you should add them one by one:
your_array_list.add("Party");
your_array_list.add("Study");
your_array_list.add("Exam");
Or much better, declare them in a constant array and add them in a loop:
String[] ITEMS = new String[]{"Party", "Study", "Exam"};
...
for(String item : ITEMS) {
your_array_list.add(item);
}
Also, consider using an enum instead of a constant array. But it depends on what you are specifically programming, of course.

Convert from string[] to string Android

How can I convert from String[] to String only? So I can assign orderan to items and i can put it into my listview.
Here's my code.
MyClass class= new MyClass();
String orderan =class.getName();
String[] items = orderan; **the problem here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
It appears that you simply want this:
String orderan = class.getName();
String[] items = {orderan};
Or even:
String[] items = {class.getName()};
ArrayAdapter wants an array, but you're trying to assign the string orderan into the array items. Instead, make a new array, and add orderran into it. Something like:
String[] items = new String[1];
items[0] = orderan;
You have to create an Array and add the string to it.
String[] items = new String[1];
items[0] = orderan;
you can do it in one line
String[] items = {orderan};

Cannot cast from String[] to ArrayList<String>

I have a resource string array that I want to put into an ArrayList, however the datatype that comes back is a String array. The string[] cannot be directly cast to ArrayList w/o receiving the following error:
Cannot cast from String[] to ArrayList
How to I convert the String[] datatype to ArrayList?
Edit
I have an adapter whose constructor takes an ArrayList and a resource string-array that I want to populate it.
If you can make do with a List<String> (and not specifically an ArrayList<String>), you can use:
List<String> list = Arrays.asList(stringArray);
Otherwise, you can do this:
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
However, the latter is less efficient (in both time and object creation count) than the other suggested solutions. Its only benefit is that it keeps the code down to one line and is easy to comprehend at a glance.
public ArrayList<String> arrayToArrayList(String[] array){
ArrayList<String> arrayList = new ArrayList<String>(array.length);
for(String string : array){
arrayList.add(string);
}
return arrayList;
}
how about this
import java.util.Collections;
String[] myStringArray = new String[] {"foo", "bar", "baz"};
List myList = new ArrayList(myStringArray.length);
Collections.addAll(myList, myStringArray);

How to remove duplicate value in IntegerArray in android app development

I would like to know how to remove duplicate values in Integer Array.
I think you may face This question in all languages.
But in android I don't know how to achieve this.Can anyone please help me to fix this issue.
Thanks in advance...
Be sure your array is Integer type not int
Integer[] array; // Your integer array...
Set<Integer> set = new HashSet<Integer>();
Collections.addAll(set, array);
val finalArrayList: ArrayList<String>
val arrayList = arrayListOf<String>()
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
finalArrayList = arrayList.toSet().toList() as ArrayList<String>
Answer
arrayList: [ABC, XYZ, ABZ, ABC, XYZ, ABZ]
finalArrayList: [ABC, XYZ, ABZ]
Try this Code
public static void removeDuplicateWithOrder(ArrayList arlList)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
arlList.clear();
arlList.addAll(newList);
}

Categories

Resources