We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Is there a simple way to return a string from an arrayList? Do you have to first convert ArrayList to String? How do we do this?
For instance....
String compareNodes () {
for(int z=0; z < finalNodes.size(); z++){
for(int x=0; x < firstNodes.size(); x++){
if (finalNodes.get(z).equals(firstNodes.get(x))) {
println("We found link " + firstNodes.get(x) + " in finalNodes: " + finalNodes.get(z) + " that is numberArray " + z );
println("how we go here 1...");
String holder = (String)firstNodes.get(x);
foundConnection();
exit();
return holder;
}
}
}
}
Answers
it's called 'casting'
this is fine:
String holder = (String)firstNodes.get(x);
however, you can also tell it the type when you create the ArrayList:
List<String> myList = new ArrayList<String>();
then it knows that anything fetched by get(i) will be a String so you can just
String holder = firstNodes.get(x);
without the need to cast.
In the above code, the second line will never be reached...
You can also use the toString() method, if what you put in the list isn't already strings:
As said, it depends on what these lists hold.