《Guava Bytes类》学习笔记

Bytes是byte的基本类型实用工具类。

 

 

类声明

 

以下是com.google.common.primitives.Bytes类的声明:

 

@GwtCompatible
public final class Bytes
   extends Object

 

 

方法

 

 

 

继承的方法

 

这个类继承了以下类方法: java.lang.Object
  

 


Bytes 示例

 

使用所选择的编辑器创建下面的java程序 C:/> Guava


GuavaTester.java

 

import java.util.List;
import com.google.common.primitives.Bytes;

public class GuavaTester {
   public static void main(String args[]){
      GuavaTester tester = new GuavaTester();
      tester.testBytes();
   }

   private void testBytes(){
      byte[] byteArray = {1,2,3,4,5,5,7,9,9};

      //convert array of primitives to array of objects
      List<Byte> objectArray = Bytes.asList(byteArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      byteArray = Bytes.toArray(objectArray);
      System.out.print("[ ");
      for(int i = 0; i< byteArray.length ; i++){
         System.out.print(byteArray[i] + " ");
      }
      System.out.println("]");
      byte data = 5;
      //check if element is present in the list of primitives or not
      System.out.println("5 is in list? "+ Bytes.contains(byteArray, data));

      //Returns the index		
      System.out.println("Index of 5: " + Bytes.indexOf(byteArray,data));

      //Returns the last index maximum		
      System.out.println("Last index of 5: " + Bytes.lastIndexOf(byteArray,data));				
   }
}

 

 

验证结果

 

使用javac编译器编译如下类

 

C:\Guava>javac GuavaTester.java

 

现在运行GuavaTester看到的结果

 

C:\Guava>java GuavaTester

 

看到结果

 

[1, 2, 3, 4, 5, 5, 7, 9, 9]
[ 1 2 3 4 5 5 7 9 9 ]
5 is in list? true
Index of 5: 4
Last index of 5: 5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值