Ints join() function in Java

Java 8Object Oriented ProgrammingProgramming

The join() method of Ints class returns a string containing the supplied int values separated by separator. The syntax is as follows −

public static String
   join(String separator, int[] arr)

Here, separator parameter is something that should appear between consecutive values, whereas arr is an array of int values.

Let us first see an example −

Example

import com.google.common.primitives.Ints;
class Demo {
   public static void main(String[] args) {
      int[] myArr = { 10, 20, 30, 40, 50, 60,70, 80, 90, 100 };
      System.out.println(Ints.join("-", myArr));
      int index = Ints.indexOf(myArr, 40);
      if (index != -1) {
         System.out.println("Element 40 is in the array at position " + (index+1));
      } else {
         System.out.println("Element 40 is not in the array");
      }
   }
}

Output

10-20-30-40-50-60-70-80-90-100
Element 40 is in the array at position 4
raja
Updated on 24-Sep-2019 07:13:04

Advertisements