Java 中的 Ints 类
Ints 类是基本类型 int 的实用程序类。让我们看看类声明 −
@GwtCompatible public final class Ints extends Object
示例
让我们看一个执行连接的某些方法的示例。Ints 类中的 concat() 函数用于连接作为参数传递的数组 −
import com.google.common.primitives.Ints; import java.util.*; class Demo { public static void main(String[] args) { int[] myArr1 = { 100, 150, 230, 300, 400 }; int[] myArr2 = { 450, 550, 700, 800, 1000 }; System.out.println("Array 1 = "); for(int i=0; i < myArr1.length; i++) { System.out.println(myArr1[i]); } System.out.println("Array 2 = "); for(int i=0; i < myArr2.length; i++) { System.out.println(myArr2[i]); } int[] arr = Ints.concat(myArr1, myArr2); System.out.println("Concatenated arrays = "+Arrays.toString(arr)); } }
输出
Array 1 = 100 150 230 300 400 Array 2 = 450 550 700 800 1000 Concatenated arrays = [100, 150, 230, 300, 400, 450, 550, 700, 800, 1000]
示例
让我们看另一个示例 −
import java.util.List; import com.google.common.primitives.Ints; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); tester.testInts(); } private void testInts() { int[] intArray = {1,2,3,4,5,6,7,8,9}; //convert array of primitives to array of objects List<Integer> objectArray = Ints.asList(intArray); System.out.println(objectArray.toString()); //convert array of objects to array of primitives intArray = Ints.toArray(objectArray); System.out.print("[ "); for(int i = 0; i < intArray.length ; i++) { System.out.print(intArray[i] + " "); } System.out.println("]"); //check if element is present in the list of primitives or not System.out.println("5 is in list? " + Ints.contains(intArray, 5)); //Returns the minimum System.out.println("Min: " + Ints.min(intArray)); //Returns the maximum System.out.println("Max: " + Ints.max(intArray)); //get the byte array from an integer byte[] byteArray = Ints.toByteArray(20000); for(int i = 0; i < byteArray.length ; i++) { System.out.print(byteArray[i] + " "); } } }
输出
[1, 2, 3, 4, 5, 6, 7, 8, 9] [ 1 2 3 4 5 6 7 8 9 ] 5 is in list? true Min: 1 Max: 9 0 0 78 32
广告