Is it possible to iterate over a list of arrays with elements of different data types and expose them?
Without generics, you can create ArrayList
with elements of different types. I want to iterate over it and erase items. I cannot use for-each-loop because it wants a specific type. I tried Iterator
it but was not successful.
I have two questions:
-
Is it possible to iterate over a list of arrays and expose (for example
System.out.println
) all elements no matter what type they are? -
Is it possible to iterate over a list of arrays and select only elements with a specific type (for example, only strings)?
source to share
Is it possible to iterate over a list of arrays and print (for example with System.out.println) all elements no matter what type of file they are?
You can of course iterate over the list (or arraylist) of the class Object
and do what you want.
Is it possible to iterate over a list of arrays and expose only those elements that are of a certain file type (for example, only strings)?
Yes, you can use instanceof
and perform specific actions for specific classes.
Usage example:
List<Object> genericList = new ArrayList<>();
genericList.add("test");
genericList.add(2);
genericList.add('c');
for (Object object: genericList) {
// "Put" out object (question 1)
System.out.println(object);
// Check object type (question 2)
if (object instanceof AnyClass) {
//doSomething()
}else if (object instanceof AnotherClass){
//doSomethingElse()
}
}
source to share
Sure!
The method is toString
defined in the class Object
. The class Object
is the base class of each user-defined class. You can easily write:
for (Object item: itemList) {
// prints all items
System.out.println(item);
if (item instanceof YourDesiredClass) {
YourDesiredClass specificItem = (YourDesiredClass) item;
//doSomethingElse(specificItem)
}
}
source to share
Is it possible to iterate through a list of arrays and put out (like with System.out.println) all elements regardless of the file type they are?
Yes, you can use the Object class
List<Object> myList = new ArrayList<>();
myList.add("Hello World"); // string
myList.add(Math.PI); // a double
myList.add(188); // an int
myList.add(9099099999999L); // a Long
// for java8
myList.forEach(System.out::println);
//before java8
for (Object object : myList) {
System.out.println(object);
}
Is it possible to iterate over a list of arrays and expose only elements that are of a specific file type (for example, only strings)?
Yes, you can use an iterator and get an object that checks it against the class you want.
Iterator<Object> it = myList.iterator();
while (it.hasNext()) {
Object x = it.next();
if (x instanceof String) {
System.out.println("this is a String: " + x);
}
}
source to share
As far as I know, yes.
You can do ArrayList
which contains objects (see Java class Object), because every class you define in Java at least extends the Object class, which is a superior class.
Now let me answer your questions:
-
Yes it is. Each object in the list knows which class is an instance and has a method
toString()
. When you loop through the ArrayList and calltoString()
for each object, the most specific method istoString()
called. For example, if an Integer instance (let's call it a number) and you passed it to an object, call itnumber.toString();
, although the compiler now treats that number as an object, it will call a methodtoString()
from the Integer class. This is called dynamic polymorphism -
yes you can check which class is an instance of an object. Each of these objects has this information; casting it to Object is like telling the compiler "here's an object, I want you to treat it as an instance of Object" - just like putting points in the compiler. And the object knows what class it is, so you can just ask, for example:
if(myObject instanceof String){ //do something; }
Hope it helped, I've tried to explain it as best I can so you can understand what's going on "under the hood" :)
source to share
-
Just an object
new ArrayList<Object>().iterator().forEachRemaining(element -> { System.out.println(element); });
-
Specific type
new ArrayList<Object>().stream().filter(element -> element instanceof String).iterator() .forEachRemaining(System.out::println);
Edit: This answer requires Java 8
source to share
You can always use the type All objects with common properties. The latter will always be Object, since each class extends Object.
But since we don't like to throw basically a better build approach for this base class:
public abstract class FileType
{
public abstract String getTypeName();
public abstract String getTypeDescription();
}
public class JSON extends FileType
{
@Override
public String getTypeName()
{
return "JSON";
}
@Override
public String getTypeDescription()
{
return "JavaScript Object Notation";
}
}
public class TXT extends FileType
{
@Override
public String getTypeName()
{
return "TXT";
}
@Override
public String getTypeDescription()
{
return "Textfile";
}
}
Now you can create a FileType list and use its methods:
List<FileType> fileTypes = new ArrayList<>();
list.add(new JSON()); // JSON fits good in here
list.add(new TXT()); // TXT also
for (FileType fileType : list)
{
System.out.println(fileType.getTypeName()); // have the FileType-Methods savely available
}
source to share