Split strings on string array - android

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.

Related

How to get a random value from a string array without repetition?

How to get a random value from a string array in android without repetition?
I have array in String.xml file as below -
<string-array name="msg">
<item>Cow</item>
<item>Pig</item>
<item>Bird</item>
<item>Sheep</item>
</string-array>
I am selecting random string by using following code -
String[] array = Objects.requireNonNull(context).getResources().getStringArray(R.array.msg);
String Msg = array[new Random().nextInt(array.length)];
Can anyone help me please? Thanks is advance...!
Can you just do something like this:
Collections.shuffle(copyOfArray);
Then loop through that?
for (int i = 0; i < copyOfArray.size(); i++) {
println(copyOfArray.get(i))
}
try this -
array = Objects.requireNonNull(context).getResources().getStringArray(R.array.msg);
//String msg = array[new Random().nextInt(array.length)];
LinkedList<String> myList = new LinkedList<String>();
for (String i : array)
myList.add(i);
Collections.shuffle(myList);
for (int i = 0; i < myList.size(); i++) {
String msg=myList.get(i);
}
Try this solution,
LinkedList<String> myList = new LinkedList<String>();
String[] words = { "Cow", "Pig", "Bird", "Sheep" };
for (String i : words)
myList.add(i);
Collections.shuffle(myList);
Then,
sampleText.setText(myList.pollLast());
pollLast() in LinkedList will retrieves and removes the last element of this list, or returns null if this list is empty.
try this.
int max = array.length() - 1;
int min = 0;
String Msg = array[new Random().nextInt(max - min + 1) + min];
First convert your String resource array to ArrayList
Fill value from current ArrayList to HashSet and convert that HashSet to newly ArrayList
Now shuffle that new ArrayList

Removing Key String from JSON Array when converting to List

I am rather new to JSON at the moment, but I need to convert a JSON response that contains the same key, but different values to an ArrayList to use it with my spinner.
I tried it like here: Converting JSONarray to ArrayList
But i get the whole json string, but just need the value part.
I can't figure out how to do this and found no answer that worked for me :/
What I want would be a List like:
City1
City2
City3
But i have in my spinner:
{"city":"name1"}
{"city":"name2"}
{"city":"name3"}
Code I have is:
JSONArray obj = new JSONArray(response);
Spinner availableCitySpin;
availableCitySpin = (Spinner) findViewById(R.id.avCitySp);
List<String> cityValues = new ArrayList<String>();
if (jarr != null) {
for (int i=0;i< jarr.length();i++){
cityValues.add(jarr.getString(i).toString());
}
}
ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cityValues);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
availableCitySpin.setAdapter(cityAdapter);
availableCitySpin.setSelection(0);
Change your code to something like this:
...
for (int i=0;i< jarr.length();i++){
JSONObject cityObject = jarr.getJSONObject(i);
cityValues.add(cityObject.getString("city"));
}
...
Try this:
First use split
For example: String[] result = splits[0].split(":");
you will get two item in array result. result[0]= {"city" and result[1] = "name1"}
If you want to get the key use result[0]
remove sign from result[0] using replace. Example: data = result[0].replace("\"","").replace("{","");
use it in loop, should work

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.

Android: Merging 2 ArrayList with same index

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();

How to add one arraylist to other arraylist in android?

I have created 3 arraylists and stored data in it. I need to pass one single arraydata to other page and so I have added my individual arrays to the main array. But when I added one array to other the data is not being aded to that array. Here is my code:
static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3;
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist = new ArrayList<String>();
stringList3 = new ArrayList<ArrayList<String>>();stringList3 = dbAdapter.selectRecordsFromDBList(query2, null);
for (int i = 0; i < stringList3.size(); i++) {
optionlist = stringList3.get(i);
System.out.print("option list size");
System.out.print(optionlist.size());
stringList1.add(optionlist);
System.out.println("total stringlist1"+stringList1.get(0));
I am getting the stringList1 array values from Mypage and accessing that in new page. In the new page I am trying to add the optionlist array to stringList1 by giving stringList1.add(optionlist) but the data is not adding. Where I went wrong? Please help me regarding this... Thanks in Advance
Use addAll() It appends all of the elements in the specified ArrayList to the end of this list, in the order that they are returned by the specified Collection's Iterator.
use stringList1.addAll(optionlist); instead of stringList1.add(optionlist);
After seeing your code I can guess that you have problem that elements of arrayList is not copy in another arrayList.If this is your issue then change your code like below
static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3;
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist;
stringList3 = new ArrayList<ArrayList<String>>();
stringList3 = dbAdapter.selectRecordsFromDBList(query2, null);
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
System.out.print("option list size");
System.out.print(optionlist.size());
stringList1.add(optionlist);
System.out.println("total stringlist1"+stringList1.get(0));
}
The problem seems that you are initializing your
ArrayList<String> optionlist;
only once that is outside the loop. So, in that case only the first ArrayList gets stored. So, initialized the ArrayList<String> optionlist inside the loop.
ArrayList<String> optionlist;
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
stringList1.add(optionlist);
}

Categories

Resources