Can the "+=" operator be used to add multiple elements to an array at once?

QuestionsQuestions8 SkillsProShell ArraysAug, 21 2025
0148

No, the += operator cannot be used to add multiple elements to an array at once in Java. The += operator is typically used for appending a single value to a variable, but arrays in Java have a fixed size once they are created.

To add multiple elements to an array, you would need to create a new array with a larger size and copy the existing elements along with the new ones into it. Alternatively, you can use an ArrayList, which allows dynamic resizing and provides methods like addAll() to add multiple elements easily.

Here's an example using ArrayList:

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("apple");
arrayList.add("banana");

// Adding multiple elements
Collections.addAll(arrayList, "orange", "grape", "melon");

In this example, addAll() is used to add multiple elements to the ArrayList.

0 Comments

no data
Be the first to share your comment!