4.24Java包装类学习要点

1.Collections算法类;

排序   Collections.sort();

查找   Collections.binarySearch();

最大值/最小值判断   最大值 Collections.max();    最小值 Collections.min();

代码如下:

ArrayList list=new ArrayList();

list.add("ab");

list.add("gh");

list.add("oj");

list.add("lh");

//将集合的元素进行排序

Collections.sort(list);

//查找集合的元素的位置

int a=(Collections.binarySearch(list,"gh"));

System.out.println(a);

//取最大值和最小值

//最大值
 Collections.max(list);
 System.out.println(Collections.max(list));

//最小值

Collections.min(list);

System.out.println(Collections.min(list));

枚举类型;关键字enum 定义是由一组固定常量组成的,常量之间用逗号隔开

代码:

public enum Level {
//常量
  L1,L2,L3
}
public class Text {
  public void showInfo(Level no){
   switch (no) {
  case L1:
   System.out.println("大数据开发工程师");
   break;
  case L2:
   System.out.println("大数据挖掘师");
   break;
  case L3:
   System.out.println("大数据架构师");
   break;
  }
  }
 public static void main(String[] args) {

  Text t=new Text();
  t.showInfo(Level.L1);
 }
}

2.包装类;

基本数据->包装类

int a=1;

Integer num=new Integer(1);

System.out.println(num);

//包装类->基本数据

Double num=new Double(58.5);

double a=num.DoubleValue();

System.out.println(a);

3.4.parsexxx():把字符串转换为相应的基本数据类型数据(character 唯一单字符)除外(字符串->基本数据类型)
int num=Integer.parseInt("36");
boolean bool=Boolean.parseBoolean(false);
character只能是这种形式
Character char=new Character('x');

4.随机产生的方式:

Math.Randow() 代表是0~1的随机数字

(int)(Math.Randow()*10)  代表的是随机产生1~10整数型的数字

代码如下1,

Scanner input=new Scanner(System.in);
 System.out.println("请输入五位数字");
 int num=input.nextInt();
 //Math.random()产生0-1的随机数
 int a=(int)(Math.random())*10;
 if(num%10==a){
  System.out.println("恭喜你中奖");
 }
 if(num/1000%10==a){
  System.out.println("恭喜你中奖");
 }else{
  System.out.println("谢谢参与");
 }

2.//构建对象时,产生随机生成数字的种子
 int num=(int)(Math.random()*10);
 //生成随机数的方式
 Random random=new Random(num);
 Random random1=new Random(num);
 //random.nextInt() 为 java.util.Random类中的方法;
 int a=random.nextInt(10);
 int b=random1.nextInt(10);
 
 System.out.println(a+"\t"+b);

5.Math类;提供一些基本数据运算和集合运算的方法,此类的所有的方法都是静态的

//产生随机数
 int a=(int)(Math.random()*10);
 System.out.println("我行我素的购物系统");
 Scanner input=new Scanner(System.in);
 System.out.println("请输入四位数");
 int num=input.nextInt();
 //获取百位的数字
 int baiwei=num/100%10;
 if(baiwei==a){
  System.out.println("恭喜你中奖啦!");
 }else{
  System.out.println("很抱歉,感谢你的参与!");
 }

6.String类;

String在Java中,字符串被作为String类型的对象来处理,位于Java.lang包中,提供了许多方法;列如可以获得字符串的长度,对字符串进行比较,连接两个字符串以及提供字符串中的某一份部分

字符串长度;length() 

格式;字符串.length();

length()方法返回字符串的长度

字符串比较;

字符串1.equals(字符串2);





















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值