I'm currently trying to iterate through an arraylist of sets that contain strings. It looks like this:
ArrayList<Set<String>> e = new ArrayList<Set<String>>(Size);
for(int i = 0; i < e.size(); i++) {
for(Object obj: e) {
System.out.println(e);
}
}
I'm trying to access and modify the strings inside the set (Printed beforehand to see if it worked), but I can't find a way to get to them. Any ideas?
You cannot modify a String; if you want to change one of the Strings in the Set
s, you have to remove it and to add the new one.
Regarding the (simple) iteration:
for( var set : e )
{
for( var string : set )
{
out.println( string );
}
}
If you want to change the contents of your data structure:
for( var set : e )
{
Set<String> newStrings = new HashSet<>();
for( var i = set.iterator(); i.hasNext(); )
{
var string = i.next();
if( isInvalid( string ) )
{
i.remove();
newStrings.add( calculateNewString( string ) );
}
}
set.addAll( newStrings );
}
ArrayList<Set<String>> e = new ArrayList(size);
//Fill your sets with data here
for(Set<String> set : e) { //Iterate through the Arraylist getting every set
for(String element : set) { //Iteretae through the current Set getting every String
System.out.println(element);
}
}
Hey I can help you with the accessing part, therefore you shuold use a foreach loop like this
ArrayList<Set<String>> e = new ArrayList<Set<String>>();
for (Set<String> oneSet : e) {
for (String oneString : oneSet) {
oneString
//do something...
}
}
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.