OneBite.Dev - Coding blog in a bite size

add new item in array in java

Code snippet on how to add new item in array in java

  // Create an ArrayList of Strings 
  ArrayList<String> fruits = new ArrayList<String>();
    
  // Add items to ArrayList
  fruits.add("Apple"); 
  fruits.add("Banana"); 
  fruits.add("Strawberry"); 
  fruits.add("Cherry"); 

This code sample creates an ArrayList of Strings called “fruits”. An ArrayList is a type of array that can be modified and can include multiple data types. After creating the ArrayList, you can add items to it using the add() method. In this case, we’re adding four different types of fruits. Each is added to the array one at a time. Finally, the ArrayList “fruits” now contains four items which can be accessed one at a time.

java