join string array java – Java Program to Join Elements of String Array with Delimiter

Join string array java: In the previous article, we have seen  Java Program to Find Square Root of Each Element of the Array

In this article we are going to see how to Join Elements of String Array using Java programming language.

Java Program to Join Elements of String Array with Delimiter

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to join elements of string array with delimiters.

Method-1: Java Program to Join Elements of String Array with Delimiter By Using String.join() Method and Static Initialization of Array Elements

Approach:

  1. Initialize the array.
  2. Join the array elements using String.join() method.
  3. Print the joined String.

Program:

public class Main 
{
    public static void main(String[] args) 
    {
        // initialize string array
        String[] strArray = { "You", "Are", "Learning", "From", "BtechGeeks" };
        //calling  joinElements() user defined method
        joinElements(strArray);
    }
    
    //joinElements() user defined method to join string elements
    static void joinElements(String[] strArray) {
        // join elements of array
        //By using String.join() inbuilt finction
        //Here delimeter is '-'
        String str = String.join("-", strArray);
        System.out.println(str);
    }

}

Output:

You-Are-Learning-From-BtechGeeks

Method-2: Java Program to Join Elements of String Array with Delimiter By Using String.join() Method and Dynamic Initialization of Array Elements

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Initialize the array.
  6. Join the array elements using String.join() method.
  7. Print the joined String.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // extra sc.nextLine(); to remove the newline character
        sc.nextLine();
        // initialize array with size n
        String[] strArray = new String[n];
        // take input from user for array elements
        System.out.println("Enter array elements: ");
        for (int i = 0; i < n; i++) {
            strArray[i] = sc.nextLine();
        }
        //Calling joinElements() user defined method
        joinElements(strArray);
    }
    
    //joinElements() user defined method to join string elements
    static void joinElements(String[] strArray) 
    {
        // join elements of array
        String str = String.join("-", strArray);
        System.out.println("Joined elements: " + str);
    }

}
Output:

Enter the size of array: 3
Enter array elements: 
BtechGeeks
is
Best
Joined elements: BtechGeeks-is-Best

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: