Add Suffix to array elements in Java

Hello Coder’s in this Tutorial we will learn how to add Suffix to each element of an Array in Java.

Explanation:

While accessing the array, update the element by adding the Suffix to all the elements.

Here we are having an array off names, we need to add suffix to each name present in an ArrayList. To get the results we will use for loop

and then increment and add the suffix to the existing arrays.

Steps to add any string to end of array elements in Java

  • Create an array with elements.
  • strArray is a collection. Str is a variable name.
  • Create a for loop. To go to the next element by incrementing.
  • It will add a Suffix to the existing elements in an array.
  • Now calling for each loop. We will create a variable iteration
  • For (type var : collection )
  • Then we will print the value.

JAVA PROGRAM TO ADD SUFFIX TO ARRAY ELEMENTS

class Main {
  public static void main (String[] args) {
    String[] strArry = {"Albert Einstein", "Stephen Hawking", "Nikola Tesla"};
    for (int i=0 ; i < strArry.length; i++) {
      strArry[i] =  strArry[i] + " is Famous ";
    }
    
    for (String str: strArry) {
      System.out.println (str);
    }
  }
}

INPUT:

{"Albert Einstein", "Stephen Hawking", "Nikola Tesla"}
SUFFIX:
is Famous

OUTPUT:

Albert Einstein is Famous
Stephen Hawking is Famous
Nikola Tesla is Famous

So for conclusion, this is the simplest way for adding a suffix to existing array elements.

You may also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *