I have one arraylist that contain two list
like this
[[asd, asswwde, efef rgg], [asd2223, asswwd2323e, efef343 rgg]]
My Code is
ArrayList<String> create = new ArrayList<String>();
ArrayList<String> inner = new ArrayList<String>();
ArrayList<String> inner1 = new ArrayList<String>();
inner.add("asd");
inner.add("asswwde");
inner.add("efef rgg");
inner1.add("asd2223");
inner1.add("asswwd2323e");
inner1.add("efef343 rgg");
create.add(inner.toString());
create.add(inner1.toString());
i have to get all value one by one of every index of that arraylist
So what is the best way to get these all value one by one.
I am using JAVA with Eclipse Mars.
Just use two nested loops:
List<List<Object>> list = ...;
for (List<Object> subList : list) {
for (Object o : subList) {
//work with o here
}
}
You may also want to consider replacing the inner lists by proper objects.
You want to loop through the outside ArrayList and then loop through each ArrayList within this ArrayList, you can do this by using the following:
for (int i = 0; i < outerArrayList.size(); i++)
{
for (int j = 0; j < outerArrayList.get(i).size(); j++)
{
String element = outerArrayList.get(i).get(j);
}
}
Here is another verison you may find easier to understand, but is essentially the same:
for (int i = 0; i < outerArrayList.size(); i++)
{
ArrayList<String>() innerArrayList = outerArrayList.get(i)
for (int j = 0; j < innerArrayList.size(); j++)
{
String element = innerArrayList.get(j);
}
}
or alternatively again using a foreach loop:
for (ArrayList<String> innerArrayList : outerArrayList)
{
for (String element : innerArrayList)
{
String theElement = element;
}
}
It might be worth noting that your ArrayList appears to contain different types of elements - is this definitely what you wanted to do? Also, make sure you surround your strings with "" unless they are variable names - which it doesn't appear so.
EDIT: Updated elements to type String as per your update.
I would also recommend you change the type of your create ArrayList, like below, as you know it will be storing multiple elements of type ArrayList:
ArrayList<ArrayList> create = new ArrayList<ArrayList>();
Try to use for loop nested in foreach loop like this:
for(List list : arrayListOfList)
{
for(int i= 0; i < list.size();i++){
System.out.println(list.get(i));
}
}
I'm not sure if the data structures are part of the requirements, but it would be better constructed if your outer ArrayList used ArrayList as the generic type.
ArrayList<ArrayList<String>> create = new ArrayList<ArrayList<String>>();
ArrayList<String> inner = new ArrayList<String>();
ArrayList<String> inner1 = new ArrayList<String>();
...
create.add(inner);
create.add(inner1);
Then you could print them out like this:
for(List list : create) {
for (String val : list) {
System.out.println(val);
}
}
Othewise, if you stick with your original code, when you add to the outer list you are using the toString() method on an ArrayList. This will produce a comma delimited string of values surrounded by brackets (ex. [val1, val2]). If you want to actually print out the individual values without the brackets, etc, you will have to convert the string back to an array (or list) doing something like this:
for (String valList : create) {
String[] vals = valList.substring(1, val.length() - 1).split(",");
for (String val : vals) {
System.out.println(val.trim());
}
}
Related
I am having issue to store all values of ArrayList<ArrayList<String>> into ArrayList<String>. Here stylistIDArray and durationArray are array of array. I want to store all their values in stylistId and duration respectively. The stylistid and duration are array of string.
Here's my attempt, but it stores only the last item of each array of array.
ArrayList<ArrayList<String>> stylistIDArray;
ArrayList<ArrayList<String>> durationArray;
stylistIDArray = differentGenderServicesAdapter.getSelectedStylistIdArray();
durationArray = differentGenderServicesAdapter.getSelectedDurArray();
ArrayList<String>stylistId = new ArrayList<>();
ArrayList<String>duration = new ArrayList<>();
for(int i=0; i<stylistIDArray.size(); i++) {
stylistId = stylistIDArray.get(i);
duration = durationArray.get(i);
}
Note : I have already tried this, but doesn't work for me.
To be generic, First let be an list of list of objects
List<List<Object>> listOfList;
That you want to put into a list of object
List<Object> result;
Note the result list will contain every object contain is the input list. This transformation will loose the information of which object was in which list.
You have to loop through the listOfList. At each loop you obtain a list of object (List<Object> listOfObject). Then loop through these lists to obtain every object (Object o). Then add these object to the result list (result.add(o)).
for(List<Object> listOfObject : listOfList) {
for(Object o : listOfObject) {
result.add(o);
}
}
In your case, the problem is that you use affectation instead of add(). At every loop this replaces the value by the new one. So at the end you have stored only the last item of each list.
stylistId=stylistIDArray.get(i); //This replace the existing stylistId
Instead try something like
ArrayList<ArrayList<String>> stylistIDArray;
ArrayList<ArrayList<String>> durationArray;
stylistIDArray = differentGenderServicesAdapter.getSelectedStylistIdArray();
durationArray = differentGenderServicesAdapter.getSelectedDurArray();
ArrayList<String> stylistId = new ArrayList<>();
ArrayList<String> duration = new ArrayList<>();
for(ArrayList<String> l : stylistIDArray) {
for(String s : l) {
stylistId.add(s);
}
}
for(ArrayList<String> l : durationArray ) {
for(String s : l) {
duration.add(s);
}
}
You doing wrong operation with arraylist, in loop you are getting data from stylistIDArray and assign to aryalist, not inserting in list, have look this
stylistIDArray=differentGenderServicesAdapter.getSelectedStylistIdArray();
durationArray=differentGenderServicesAdapter.getSelectedDurArray();
ArrayList<String>stylistId=new ArrayList<>();
ArrayList<String>duration=new ArrayList<>();
for(int i=0; i<stylistIDArray.size(); i++) {
stylistId.add(stylistIDArray.get(i));
duration.add(durationArray.get(i));
}
Hope it will help you!
I need to check if all Strings from ArrayList are present in another ArrayList. I can use containsAll but this is not what I want to achieve. Let's me show you this on example:
assertThat(firstArray).containsAll(secondArray);
This code will check if all items from one array is in another one. But I need to check that every single item from one array is contained in any place in the second array.
List<String> firstArray = new ArrayList<>;
List<String> secondArray = new ArrayList<>;
firstArray.add("Bari 1908")
firstArray.add("Sheffield United")
firstArray.add("Crystal Palace")
secondArray.add("Bari")
secondArray.add("Sheffield U")
secondArray.add("C Palace")
So I want to check if first item from secondArray is in firstArray(true) than that second(true) and third(false). I wrote the code which is doing this job but it's quite complicated and I would like to know if there is any simpler way to achieve this goal (maybe with using hamcrest matchers or something like that)
ArrayList<String> notMatchedTeam = new ArrayList<>();
for (int i = 0; i < secondArray.size(); i++) {
String team = secondArray.get(i);
boolean teamMatched = false;
for (int j = 0; j < firstArray.size(); j++) {
teamMatched = firstArray.get(j).contains(team);
if (teamMatched) {
break;
}
}
if (!teamMatched) {
notMatchedTeam.add(team);
}
}
You can do something like this
List<String> firstArray = new ArrayList<>();
List<String> secondArray = new ArrayList<>();
firstArray.add("Bari 1908");
firstArray.add("Sheffield United");
firstArray.add("Crystal Palace");
secondArray.add("Bari");
secondArray.add("Sheffield U");
secondArray.add("C Palace");
Set<String> firstSet= firstArray
.stream()
.collect(Collectors.toSet());
long count= secondArray.stream().filter(x->firstSet.contains(x)).count();
///
Map<String, Boolean> result =
secondArray.stream().collect(Collectors.toMap(s->s, firstSet::contains));
If count >0, then there are some items in second array which are not there in first.
result contains the string with its status.
Thanks
If you have space concerns like you have millions of words in one file and need to check entry of second file in first then use trie. From first make trie and check every entry of second in first.
Situation:
In your question you said that you wanted to return for each element if it exists or not, and in your actual code you are only returning a list of matching elements.
Solution:
You need to return a list of Boolean results instead, this is the code you need:
public static List<Boolean> whichElementsFound(List<String> firstList, List<String> secondList){
ArrayList<Boolean> resultList = new ArrayList<>();
for (int i = 0; i < secondList.size(); i++) {
String team = secondList.get(i);
resultList.add(firstList.contains(team));
}
return resultList;
}
Demo:
This is a working Demo using this method, returning respectively a List<Boolean> to reflects which element from the first list are found in the second.
Edit:
If you want to return the list of elements that were not found, use the following code:
public static List<String> whichElementsAreNotFound(List<String> firstList, List<String> secondList){
ArrayList<String> resultList = new ArrayList<>();
for (int i = 0; i < secondList.size(); i++) {
String team = secondList.get(i);
if(!firstList.contains(team)){
resultList.add(team);
}
}
return resultList;
}
This is the Demo updated.
I need to get a list from another list so here is what I have done :
ArrayList<String> userList = user.getListSalarieByManager(login);
ArrayList<DHDemande> demandesList;
for (int i = 0; i < userList.size(); i++) {
demandesList = d.getDemandesForManager(userList.get(i));
}
Then I need to get the data from the list demandesList but I can't get this list outside the loop because this not have been initialized.
How can I get the data from the list inside the loop ?
That is because you haven't actually initialized your second list.
ArrayList<DHDemande> demandesList;
Should be:
ArrayList<DHDemande> demandesList = new ArrayList<DHDemande>();
By the way, the way your loop is set up sets the entire demandesList every iteration. Are you perhaps looking for List#add?
Edit: to answer you question in the comments:
Yes, you can add a list to another list using ArrayList#addAll - that would look like this:
ArrayList<String> userList = user.getListSalarieByManager(login);
ArrayList<DHDemande> demandesList = new ArrayList<DHDemande>();
for (int i = 0; i < userList.size(); i++) {
demandesList.addAll(d.getDemandesForManager(userList.get(i)));
}
Edit 2: just a small note, you can replace your for loop with a for-each, since you don't really need to know the value of i (index).
Example:
for (int i = 0; i < userList.size(); i++) {
demandesList.addAll(d.getDemandesForManager(userList.get(i)));
}
Turns into:
for (String user : userList) {
demandesList.addAll(d.getDemandesForManager(user));
}
You only need to initialize the list properly, inside or outside the loop,
but it appears that you want to add elements to the list inside the loop.
I changed your iteration over the loop to the modern java list iteration style.
// initialize variables just so this example compiles
UserProvider user = new UserProvider();
Object login = null;
DHDemandeProvider d = new DHDemandeProvider(); only
ArrayList<String> userList;
userList = user.getListSalarieByManager(login);
ArrayList<DHDemande> demandesList = new ArrayList<DHDemande>(); // construct list
for (String u: userList) {
demandesList.add(d.getDemandesForManager(u)); // add elements to list
}
i am having a List of objects and i i wants to do some operation on the list in such a way that the a particular object should be shifted to list position 0 and the the object at position 0 will take place the shifted object. the diagram is as shown below.
the list is as follows
final List<Object> list= new ArrayList<Object>();
presently i have made two temporary lists as
final List<Object> temp1= new ArrayList<Object>();
final List<Object> temp2= new ArrayList<Object>();
to do the operation i am running a loop and on particular condition adding object to temp1 else adding to temp2 , something like as follows :
for (int i = 0; i < 5; i++) {
if (i==3) {
temp1.add(i);
} else {
temp2.add(i);
}
}
and finally doing
list.addAll(temp1);
list.addAll(temp2);
how to do the same logic in redundant and effective steps rather than using temp lists.
Use this swap method:
Collections.swap(List<?> list, int i, int j);
Try this code:
Object temp = list.get(0);
list.set(0, list.get(3));
list.set(3, temp);
This is what I have
ArrayList <String> cdList = new ArrayList();
Collections.addAll(cdList, "ExampleA\n"+"ExampleB\n"+"ExampleC\n"+"ExampleD");
Collections.sort(cdList, String.CASE_INSENSITIVE_ORDER);
System.out.println(cdList);
bigBox.setText("Original Order\n**************\n");
for (int i = 0; i < cdList.size(); i++) {
bigBox.setText(bigBox.getText()+""+cdList.get(i)+"\n");
}
bigBox.setText(bigBox.getText()+"\n\nSorted Order\n************\n");
Collections.sort(cdList);
for (int j = 0; j < cdList.size(); j++) {
bigBox.setText(bigBox.getText()+""+);
}
I want the 4 examples outputted in their original order, and in alphabetical order. What am I doing wrong?
You are adding only one element (String) to the list, a concatenated string.
Change this
ArrayList <String> cdList = new ArrayList();
Collections.addAll(cdList, "ExampleA\n"+"ExampleB\n"+"ExampleC\n"+"ExampleD");
to
List <String> cdList = new ArrayList<String>();
Collections.addAll(cdList, "ExampleA","ExampleB","ExampleC","ExampleD");
Read more Collections#addAll
And for showing you should use append rather than setText.
Example:
bigBox.append("Original Order\n**************\n");
for (String s : cdList) {
bigBox.append(s);
bigBox.append("\n");
}
I assume your elements are meant to be the strings "ExampleA", "ExampleB", "ExampleC", and "ExampleD". If the is the case, what you are currently doing in your call to Collections.addAll() is adding them to cdList as one long string. the + operator, when used on strings, appends them. What you probably want it to separate them with commas, so that instead of having:
Collections.addAll(cdList, "ExampleA\n"+"ExampleB\n"+"ExampleC\n"+"ExampleD");
you have:
Collections.addAll(cdList, "ExampleA\n", "ExampleB\n", "ExampleC\n", "ExampleD");