Java 中的 Ints join() 函式


Ints 類的 join() 方法返回一個包含以分隔符分隔的指定 int 值的字串。語法如下:-

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

其中,分隔符引數應顯示在連續值之間,而 arr 是一個 int 值陣列。

讓我們先看一個示例:

示例

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");
      }
   }
}

輸出

10-20-30-40-50-60-70-80-90-100
Element 40 is in the array at position 4

更新時間: 2019-09-24

254 次瀏覽

開啟您的 職業

完成課程並獲得認證

立即開始
廣告
© . All rights reserved.