栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java IO(九月总结)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java IO(九月总结)

核心类库

在九月份的学习中,主要学习了JAVA的核心类库。

一、常用类库 1、Object类(所有类层次的根,即所有类的顶级父类)
	Object 类位于 java.lang 包中,编译时会自动导入,创建一个类的时候,若没有声明继承一个明确的父类,则自动继承 Object,成为 Object 的子类。
	java.util.Objects是一个很好用的类。该类中有三个封装方法,分别是hashcode、equals、toString
	
	1.1、hashCode。
// 接收可变参数
public static int hash(Object... values) {
    return Arrays.hashCode(values);
}

//可变参数的内部是一个数组
public static int hashCode(Object a[]) {
  if (a == null)
    return 0;

  int result = 1;

  for (Object element : a)
    result = 31 * result + (element == null ? 0 : element.hashCode());

  return result;
}

public native int hashCode();

1.2、equals比较

//在Object类中定义了equals()方法
public boolean equals(Object obj) {
        return (this == obj);
}

1.3、toString
toString()方法在Object类里定义的,其返回值类型为String类型,返回类名和它的引用地址

//JDK中的源码
public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
2、math类
	     Java的Math类封装了很多用于执行基本数学运算的属性和方法,  
	 且Math 的方法都被定义为 static 形式,  通过 Math 类可以在主函数中直接调用。
//计算平方根
System.out.println(Math.sqrt(9)); // 计算9的平方根
System.out.println(Math.pow(3,4)); // 计算3的4次方
System.out.println(Math.max(25.2, 45.2));// 比较出最大值
System.out.println(Math.min(32.2, 52));//比较出最小值

//Math.ceil()  有余数取最近的大整数  
System.out.println(Math.ceil(-3.1)); //结果输出 -3.0
System.out.println(Math.ceil(3.1)); // 4.0
//Math.floor()  去余数取最近的小整数
System.out.println(Math.ceil(-3.1)); //结果输出 -4.0
System.out.println(Math.ceil(3.1)); // 3.0


//Math.rint()和Math.round():都是四舍五入,但也有区别
System.out.println(Math.rint(12.5)); 
// 12.0(返回double值)且还有个条件是 假如距离两边的整数距离相同 则取偶数
System.out.println(Math.round(12.5));
 // 13  (返回long值)正常的四舍五入

System.out.println(Math.abs(-1)); // 1
System.out.println(Math.abs(1); //1

//Math.random(): 取得一个[0, 1)范围内的随机数
System.out.println(Math.random()); // [0, 1)的double类型的数
System.out.println(Math.random() * 2);//[0, 2)的double类型的数
System.out.println(Math.random() * 2 + 1);// [1, 3)的double类型的数

Random rand = new Random();
int randNum = rand.nextInt(10)+5;//随机生成整数  范围在5到(10+5-1)
System.out.println(randNum);
3、 java.util.Arrays类

Java Arrays类仅由对数组进行操作的静态方法组成。

3.1、Arrays.copyOf()复制并改变数组长度与copyOfRange()复制并截取数组部分数据

package com.kkb;
import java.util.Arrays;
public class test
{
    public static void main(String[] args)
    {
        int a[] = {0,1,2,3,4,5,6};
        //复制a数组,且长度变为4
        int b[] = Arrays.copyOf(a, 4);//b[]={0,1,2,3}
        //复制数组a  截取2下标到(4-1)下标的数据
        int c[] = Arrays.copyOfRange(a, 2, 4);//c[]={2,3}
        for (int i = 0; i < b.length; i++) 
            System.out.print(b[i] + " ");
        System.out.println();
        for (int i = 0; i < c.length; i++) 
            System.out.print(c[i] + " ");
        System.out.println();
    }
}

3.2、数组排序Arrays.sort()
可以对指定数组的元素进行排序,还可以将给定数组的指定范围按升序排序。
例:

package com.kkb;
import java.util.Arrays;
public class test
{
    public static void main(String[] args)
    {
        // 字符数组排序
        char[] chars = {'F', 'Z', 'B', 'A', 'G'};
        Arrays.sort(chars);
        System.out.print("chars数组排序后 : ");
        for (char c : chars) {
            System.out.print(c+" ");
        }

        // 整型排序
        int[] a = {5, 2, 1, 4, 3};
        Arrays.sort(integers);
        System.out.print("a数组排序后 : ");
        for (int i : a) {
            System.out.print(i+" ");
        }

        // 给定数组的指定范围按升序排序
        int[] ints = {10,9,8,7,6,5,4,3,2,1};
        int fromIndex = 1;
        int toIndex = 3;
        Arrays.sort(ints, fromIndex, toIndex);
        System.out.print("ints按要求排序后 : ");
        for (int i : ints) {
            System.out.print(i+" ");
        }
    }
}

上述代码输出的结果
未完.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号