📜  双打班 |番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:41.301000             🧑  作者: Mango

双打班 |番石榴 |Java

Doubles是原始类型double的实用程序类。它提供了与双原语有关的静态实用方法,这些方法在双精度或数组中都没有。

宣言 :

@GwtCompatible(emulated=true)
public final class Doubles
extends Object

下表显示了番石榴双打类的字段摘要:


Guava Doubles Class 提供的一些方法是:

例外:

  • min : IllegalArgumentException如果数组为空。
  • max : IllegalArgumentException如果数组为空。
  • ensureCapacity : IllegalArgumentException如果 minLength 或 padding 为负数。
  • toArray : NullPointerException如果集合或其任何元素为空。

下表显示了 Guava Doubles Class 提供的其他一些方法:

下面给出了一些示例,展示了 Guava Doubles Class 方法的实现:
示例 1:

// Java code to show implementation
// of Guava Doubles.asList() method
  
import com.google.common.primitives.Doubles;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        double arr[] = { 2.6, 4.6, 1.2, 2.4, 1.5 };
  
        // Using Doubles.asList() method which
        // converts array of primitives to array of objects
        List myList = Doubles.asList(arr);
  
        // Displaying the elements
        System.out.println(myList);
    }
}

输出 :

[2.6, 4.6, 1.2, 2.4, 1.5]

示例 2:

// Java code to show implementation
// of Guava Doubles.toArray() method
  
import com.google.common.primitives.Doubles;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        List myList = Arrays.asList(2.6, 4.6, 1.2, 2.4, 1.5);
  
        // Using Doubles.toArray() method which
        // converts a List of Doubles to an
        // array of double
        double[] arr = Doubles.toArray(myList);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

输出 :

[2.6, 4.6, 1.2, 2.4, 1.5]

示例 3:

// Java code to show implementation
// of Guava Doubles.concat() method
  
import com.google.common.primitives.Doubles;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        double[] arr1 = { 2.6, 4.6, 1.2 };
        double[] arr2 = { 2.4, 1.5 };
  
        // Using Doubles.concat() method which
        // combines arrays from specified
        // arrays into a single array
        double[] arr = Doubles.concat(arr1, arr2);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

输出 :

[2.6, 4.6, 1.2, 2.4, 1.5]

示例 4:

// Java code to show implementation
// of Guava Doubles.contains() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        double[] arr = { 2.6, 4.6, 1.2, 2.4, 1.5 };
  
        // Using Doubles.contains() method which
        // checks if element is present in array
        // or not
        System.out.println(Doubles.contains(arr, 2.5));
        System.out.println(Doubles.contains(arr, 1.5));
    }
}

输出 :

false
true

示例 5:

// Java code to show implementation
// of Guava Doubles.min() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        double[] arr = { 2.6, 4.6, 1.2, 2.4, 1.5 };
  
        // Using Doubles.min() method
        System.out.println(Doubles.min(arr));
    }
}

输出 :

1.2

示例 6:

// Java code to show implementation
// of Guava Doubles.max() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        double[] arr = { 2.6, 4.6, 1.2, 2.4, 1.5 };
  
        // Using Doubles.max() method
        System.out.println(Doubles.max(arr));
    }
}

输出 :

4.6