I have a Class
public class DataRatesString {
private String[] ab1 = {"Auto moto sdhv davjdn adadk", "Rs. 355"} ;
private String[] ab2 = {"sjg atoiu ", "Rs. 200"} ;
private String[] ab3 = {"go to UTOP atup auto", "Rs. 3279"} ;
private String[] ab4 = {"Hid to putho", "Rs. 2424"} ;
private String[] abo5 = {"pithoo to bittu", "Rs. 8457"} ;
private ArrayList<String[]> abCollection = new ArrayList<>();
public void setAbCollection() {
abCollection.add(ab1);
abCollection.add(ab2);
abCollection.add(ab3);
abCollection.add(ab4);
abCollection.add(ab5);
}
public ArrayList<String[]> getAbCollection(){
return abCollection;
}
}
I have another class from which I am calling the list
DataRatesString dataRatesString = new DataRatesString();
dataRatesString.setAbCollection();
ArrayList<String[]> ratesarray = dataRatesString.getAbCollection();
Now I want to get all the strings at position 0 of the Arraylist String[]
I have created a method in the same class but since I am new to java I am unable to figure out exactly how to do it.
This is the method that I tried to make
private List<String> getRatesItemNamelist(){
String[] arraylist;
ArrayList<String> list = new ArrayList<>();
arraylist = new String[ratesarray.size()];
for (int i = 0; i < ratesarray.size(); i++) {
arraylist = ratesarray.get(i);
}
list.add(0, arraylist[arraylist.length]);
return list;
}
This is not working.
Try this:
private List<String> getRatesItemNamelist(){
List<String> list = new ArrayList<>();
for (String[] rates : ratesarray) {
list.add(rates[0]);
}
return list;
}
Here in the for loop, rates[0] will get the first String
from the String[]
array for each element in ratesarray ArrayList
.
So the getRatesItemNamelist()
function will return a list of String at index 0 from ArrayList ratesarray.
If i understood properly, you want all arraylist data at position 0. but i don't think you can achieve. even you want you have to add string in multiple arraylist and each string add in at 0 position respectively. why not try to make code simpler something like this
List<String> data = new ArrayList();
data.add("demo1");
data.add("demo2");
.......
Fetching data at 0 position, you could do this
data.get(0);
Fetch all data, you could use for each loop
for(String s: data){
// here is you each position's data as string
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.