当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Guava Bytes.asList()用法及代码示例


Guava字节类的Bytes.asList()方法接受字节数组作为参数,并返回具有固定大小的列表。返回的列表由作为参数传递的字节数组支持。这意味着在数组中作为参数传递的更改将反映在方法返回的列表中,反之亦然。

用法:

public static List<Byte> asList(byte… backingArray)



参数:该方法接受强制性参数backingArray,该参数是用于备份列表的字节数组。

返回值:方法Bytes.asList()返回固定大小的列表,该列表由作为该方法的参数传递的数组支持。换句话说,该方法返回数组的列表视图。

以下示例说明了上述方法的实现:

范例1:

// Java code to show implementation of 
// Guava's Bytes.asList() method 
  
import com.google.common.primitives.Bytes; 
import java.util.List; 
  
class GFG { 
  
    // Driver's code 
    public static void main(String[] args) 
    { 
  
        // Creating a Byte array 
        byte arr[] = { 1, 2, 3, 4, 5 }; 
  
        // Using Bytes.asList() method to wrap 
        // the specified primitive Byte array 
        // as a List of the Byte type 
        List<Byte> myList = Bytes.asList(arr); 
  
        // Displaying the elements in List 
        System.out.println(myList); 
    } 
}
输出:
[1, 2, 3, 4, 5]

范例2:

// Java code to show implementation of 
// Guava's Bytes.asList() method 
  
import com.google.common.primitives.Bytes; 
import java.util.List; 
  
class GFG { 
  
    // Driver's code 
    public static void main(String[] args) 
    { 
  
        // Creating a Byte array 
        byte arr[] = { 3, 5, 7 }; 
  
        // Using Bytes.asList() method to wrap 
        // the specified primitive Byte array 
        // as a List of the Byte type 
        List<Byte> myList = Bytes.asList(arr); 
  
        // Displaying the elements in List 
        System.out.println(myList); 
    } 
}
输出:
[3, 5, 7]

参考: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Bytes.html#asList(byte…)



相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | Bytes.asList() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。