Can I write a for loop that iterates over both collections and arrays?

Can I write a for loop that iterates over both collections and arrays?

Is there a possibility to check if an object is either an array or a collection with one clause? What I am trying to achieve:

Is there a possibility to check if an object is either an array or a collection with one clause? What I am trying to achieve:

Assuming arrays implement Iterable, and assuming the Object foo could be either an array or a collection, I'd like to use a code snippet like this:

if (foo instanceof Iterable) {
  for (Object f : (Iterable) foo) {
    // do something with f
  }
}

Unfortunately, an array cannot be cast to Iterable. Nor does it implement Collection. Are there any other possibilities to handle both in one loop like the above? Instead of -- of course -- using an if-else if-clause and two loops (which wouldn't be nice).

Edit: In response to these answers. I am aware of the isArray() method but in this case the casting in

...
for (Object f : (Iterable) foo) {
...

will fail. That'd a pity and a code redundancy since I would have to use two loops although a foreach-loop works both with Collections and Arrays.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Understanding Arrays in Java

An array is a group of like-typed variables that are referred to by a common name.Arrays in Java work differently than they do in C/C++. Following are some important point about Java arrays.

A Complete Java Array Tutorial

In the article, we learn about Array, features of Array, Shortcomings of Array, Types of Arrays, Array Methods and how to use the Array Class and methods using Java.

How to convert an Array to String in Java?

In this post, you'll learn the various methods to convert an Array to String in Java