Android: Merging 2 ArrayList with same index - android

I have my two arraylist and I want to merg their data like this
ArrayList prayerNames={Fajar,Zohar,Asar,Magrib,Isha};
ArrayList prayerTime={4:04am,2:58pm,4:20pm,5:09pm,8:10pm}
I want this type of merged array..
{Fajar 4:04am, Zohar 2:58pm , Asar 4:20pm, Magrib 5:09pm,Isha 8:10pm}
I have tried merged.addAll()function but its not giving me the exact same outsput.
Can any one help me out of this.
Thanks

Try this..
ArrayList<String> new_list = new ArrayList<String>();
for(int i = 0; i < prayerNames.size(); i++){
new_list.add(prayerNames.get(i)+" "+prayerTime.get(i))
}

Better will be to create map of two lists. With lists something like
Iterator<String> i1 = prayerNames.iterator();
Iterator<String> i2 = prayerTime.iterator();
while (i1.hasNext() && i2.hasNext()) {
map.put(i1.next(), i2.next());
}
if (i1.hasNext() || i2.hasNext()) complainAboutSizes();

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.

Get jsonArray objects without value name and pass it to list

I am trying to read json array without object name, and pass it to a list.
My json looks like :
"facilites": [
"Pool",
" Air Conditioning",
" Pets Allowed",
" Fitness center",
" Kitchen",
" Internet",
" Sona"
]
I am trying to retrieve it using the following code -
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list = new ArrayList<String>();
list.add(facilities);
}
Inside the main loop I put to my pojo class chalets.setList(list);
The issue is in this line list.add(facilities); it only add the last element. After looping through all, list carry sona only.
Your list should be instantiated outside the loop.
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list.add(facilities);
}
An improvement would be directly add string to list instead of capturing it into a string variable like list.add(chaletFacilities.getString(l))
Move initialization of your ArrayList outside of your loop.
Do like this
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
list.add(chaletFacilities.getString(l))
}
What you doing is initializing yourlist again and again and adding the element. So while last iteration the list is getting initialized again and only single element is being added to it.
ArrayList list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list.add(facilities);
}
You are creating the new list always. So your list size will be 1 with the last value in chaletFacilities array.
Solution: Keep your list initialization outside the for loop as below, and add all the values under the array into single list you created in the top.
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++)
{
list.add(chaletFacilities.getString(l));
}

How to save an ArrayList in SugarORM?

I don't understand how to use SugarORM to save information from an array list.
for (int i =0 ; i<price.size(); i++){
List <OrdersSugarORM> ordersGo = new ArrayList<>();
ordersGo.add(new OrdersSugarORM(article.get(i)));
OrdersSugarORM.saveInTx(ordersGo);
}
I don't understand why you think it is a good idea to save an arraylist of a single element repeatedly using the for loop.
Try this instead - build the list, then save it. (Using the correct object types, since you have not provided what OrdersSugarORM is...)
List <OrdersSugarORM> ordersGo = new ArrayList<>();
for (int i = 0; i < price.size(); i++){
ordersGo.add(new OrdersSugarORM(article.get(i)));
}
OrdersSugarORM.saveInTx(ordersGo);
And maybe you want price.get(i), otherwise make sure that price.size() == article.size()

How can I put series of numbers to a list in android?

What I'm trying to is making a list which contains series of numbers like [1,2,3,4,5,6...100].
In Java, it is simple using 'range' so I tried to find similar class in android.
Therefore, I found that some classes like Range and Intstream, but I don't know how to use them.
I'll be appreciated if you teach me how can I get my purpose, thanks.
You could write a simple function which would look like this:
public List<Integer> buildList(int maximum) {
List<Integer> list = new ArrayList();
for (int i = 1; i <= maximum; i++) {
list.add(i);
}
return list;
}
And a call which produces your desired result would look like this:
List<Integer> list = buildList(100);
If you want an array instead of a list, do this:
int[] array = list.toArray(new int[list.size()]);
int size = 100;
ArrayList<Integer> list = new ArrayList<>(size);
for (int i = 1; i <= size; i++) {
list.add(i);
Log.i("Value is = ", i+"");
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
.....
You don't need to define the size because ArrayList is dynamically
while in some other language we use list[index] to get the value.
But here we use list.get(index) to do that.

Converting an ArrayAdapter to a List<String>

I have an array adapter(string), and would like to convert it to a List<String>, but after a little googling and a few attempts, I am no closer to figuring out how to do this.
I have tried the following;
for(int i = 0; i < adapter./*what?*/; i++){
//get each item and add it to the list
}
but this doesn't work because there appears to be no adapter.length or adapter.size() method or variable.
I then tried this type of for loop
for (String s: adapter){
//add s to the list
}
but adapter can't be used in a foreach loop.
Then I did some googling for a method (in Arrays) that converts from an adapter to a list, but found nothing.
What is the best way to do this? Is it even possible?
for(int i = 0; i < adapter.getCount(); i++){
String str = (String)adapter.getItem(i);
}
Try this
// Note to the clown who attempted to edit this code.
// this is an input parameter to this code.
ArrayAdapter blammo;
List<String> kapow = new LinkedList<String>(); // ArrayList if you prefer.
for (int index = 0; index < blammo.getCount(); ++index)
{
String value = (String)blammo.getItem(index);
// Option 2: String value = (blammo.getItem(index)).toString();
kapow.add(value);
}
// kapow is a List<String> that contains each element in the blammo ArrayAdapter.
Use option 2 if the elements of the ArrayAdapter are not Strings.

Categories

Resources