get String from ArrayList - java

How to get String from ArrayList if my code like
ArrayList<String> PItoList = new ArrayList();
for (int i = 0; i < AllPunchList.size(); i++) {
PItoList.add(AllPunchList.get(i).toString());
}
I want to split item in ArrayList one by one.

You could do something like this:-
for(String eachString : PItoList){
// eachString.split(regex);
}
Traverse through each element in the List and do whatever you want to do in with each Element!

Try this :-
ArrayList<String> PItoList = new ArrayList<String>();
for (int i = 0; i < AllPunchList.size(); i++) {
PItoList.add((String)AllPunchList.get(i));
}
In the above example I have printed all elements in ArrayList. In this manner you can get all elements.
Then why you need split ?
Hope it will help you.

Related

Break ArrayList into ArrayList of ArrayLists dynamically

I am trying to make a method to breakdown an arrayList of objects into multiple arrayLists based on an attribute value of the myObj class.
private static ArrayList<ArrayList<Ticket>> splitList(ArrayList<Ticket> arrayList){
ArrayList<ArrayList<Ticket>> smallLists = new ArrayList<>();
for(int i = 0; i < arrayList.size(); i++){
for(Ticket eachTick: Ticket.getTickets()){
if(arrayList.get(i).getCategory().equals(eachTick.getCategory())){
smallLists.add(...);
}
}
}
return smallLists;
}
If there is a better way to do what I am attempting, please advise me.
Are you trying to create a list for each ticket category found in the input list? If that's the case, then I agree with Dawood ibn Kareem: use a map that looks up the proper list based on the category. Something like this:
private static ArrayList<ArrayList<Ticket>> splitList(ArrayList<Ticket> inputList) {
Map<String, ArrayList<Ticket>> map = new HashMap<>();
// your ticket categories here
// (array used for simplicity; you should use a Set, and you should also maintain the valid categories inside your Ticket class, not here)
String[] categories = new String[] {'a', 'b', 'c' };
for (int i = 0; i < categories.length; i++) {
map.put(categories[i], new ArrayList<Ticket>());
}
for (int i = 0; i < inputList.size(); i++) {
Ticket t = inputList.get(i);
map.get(t.getCategory()).add(t);
}
// Convert to list of lists
return new ArrayList<>(map.values());
}

How to search a String for characters and replace those characters with new ones?

I want to go through an ArrayList within a for loop
and check if a String contains a character A
i.e.:
String = "A"
and replace that character with (say) the characters A&%
so that now,
String = "A&%"
and going through again, it sees another A
so now it should be
String = "A&%&%"
so in a sense it's a rule:
replace all A occurrences with A&%
ArrayList<String> myList = new ArrayList<>();
myList.add("A");
for(int i = 0; i < myList.size(); i++){
if(myList.contains('A')){
myList.add("A&%");
}
System.out.println(myList);
}
You should use this piece of code instead:
ArrayList<String> myList = new ArrayList<>();
myList.add("A");
for(int i = 0; i < myList.size(); i++) {
String s = myList.get(i);
if (s.contains("A")) {
s = s.replace("A", "A&%");
myList.set(i, s);
}
System.out.println(myList);
}
I want it to go through the ArrayList with a for loop and check if the String contains a character 'A'
You need to use get(i) because you want to check the string contains, not the list contains.
ArrayList<String> myList = new ArrayList<>();
for(int i = 0; i < myList.size(); i++){
if(myList.get(i).contains("A")) {
and replace that character with say characters A&%
You need to update the string in the list, not add to the list
myList.set(i, myList.get(i).replace("A", "A&%"));
}
}
System.out.println(myList);

ArrayList iteration specific list

I have an arraylist in which
ArrayList<ArrayList<String>> listofItems=new ArrayList<ArrayList<String>>();
in the list of items I have items like this
[[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22]]
how to iterate and store in another array these values split into two like
[1,2,3,4,5,6,7,8,9,10,11] and [11,12,13,14,15,16,17,18,19,20,21,22]. I have used advanced for loop
for(ArrayList<String> list:listofItems)
{
for (String s:list)
{
//I dont know how to add logic here.
}
}
I am assuming here that both lists will also hold string type data.If you want Integer type,you can parse while adding.
ArrayList<ArrayList<String>> listofItems = new ArrayList<ArrayList<String>>();
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
for (ArrayList<String> list : listofItems) {
for (String s : list) {
// there shud be some condition how much elements you want in a
// list or some condition
// to decide in which list we need to add item
if (list.size() < 12)
list1.add(s);
else
list2.add(s);
}
}
Use something like that:
if (yourlist.size()>0) {
List<String> first = yourList.subList(0, (int)Math.ceil(yourlist.size()/2.0));
List<String> second = yourList.subList((int)Math.ceil(yourlist.size()/2.0), yourlist.size());
}
Would be separate your list in 2 lists divided in the half.
You can use this code to store your list into another list.
List<String> stringList = new ArrayList<String>();
for(ArrayList<String> list:listofItems) {
for (String s:list) {
stringList.add(s);
}
}
In this case, when you add info to listOfItems, you should try example like this
for(int i = 0; i< 2; i++){
for(int j = 0; j < n; j++){
/// add util break n
lists.get(i).add(strTemp);
}
}
You can change i < 2 into any number you want. I only make this sample for you.

Extract String arrays from List

Developing a Java Android App but this is a straight up Java question i think.
I have List declared as follows;
List list= new ArrayList<String[]>();
I want to extract each String [] in a loop;
for(int i=0; i < list.size(); i++) {
//get each String[]
String[] teamDetails = (String[])list.get(i);
}
This errors, I am guessing it just doesn't like me casting the String[] like this.
Can anyone suggest a way to extract the String[] from my List?
Use a List<String[]> and you can use the more up-to-date looping construct:
List<String[]> list = new ArrayList<String[]>();
//I want to extract each String[] in a loop;
for ( String[] teamDetails : list) {
}
// To extract a specific one.
String[] third = list.get(2);
Try declaring the list this way
List<String[]> list = new ArrayList<>();
for(int i = 0; i < list.size(); i++) {
//get each String[]
String[] teamDetails = list.get(i);
}
Moreover the call of your size function was wrong you need to add the brackets
/*ArrayList to Array Conversion */
String array[] = new String[arrlist.size()];
for(int j =0;j<arrlist.size();j++){
array[j] = arrlist.get(j);
}
//OR
/*ArrayList to Array Conversion */
String frnames[]=friendsnames.toArray(new String[friendsnames.size()]);
In for loop change list.size to list.size()
And it works fine Check this https://ideone.com/jyVd0x
First of all you need to change declaration from
List list= new ArrayList<String[]>(); to
List<String[]> list = new ArrayList();
After that you can do something like
String[] temp = new String[list.size];
for(int i=0;i<list.size();i++)`
{
temp[i] = list.get(i);
}

Convert JSON array of strings to Java?

I am trying to convert a JSON String array to Java which is given from this piece of javascript:
javaFunction(["a1", "a2"]); <--- This is called in javascript
In Java
public static void javaFunction(<What here?> jsonStringArray){ //<---- This is called in Java
//Convert the JSON String array here to something i can iterate through,
//For example:
for(int index = 0; index < convertedArray.length; index++){
System.out.println(convertedArray[index];
}
}
You can use this :
List<String> list = new ArrayList<String>();
for(int i = 0; i < jsonStringArray.length(); i++){
list.add(jsonStringArray.getJSONObject(i).getString("name"));
}
I solved it using this:
In the JavaScript:
javaFunction(JSON.Stringify(["a1", "a2"]));
In Java
public static void javaFunction(String jsonArrayString){
JSONArray jsonArray = new JSONArray(jsonArrayString);
List<String> list = new ArrayList<String>();
for(int i = 0; i < jsonArray.length(); i++){
list.add(jsonArray.getString(i));
}
}
Trim first and last bracket;
i.e.
jsonArrayString = jsonArrayString.subStr(1,jsonArrayString.length()-1);
you will get "a1","a2","a3","a4"
Then use String's split function.
String[] convertedArray= jsonArrayString.split(",");

Categories