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

JAVA中的常用类

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

JAVA中的常用类

包装类:Byte Boolean Character Short Inter Long Float Double

 

包装类怎么用:

基本数据类型à包装类型  此过程被称之为装箱

包装类型à基本数据类型   此过程被称之为拆箱

  

      //boolean Boolean之间的相互转换

      //boolean-->Boolean  装箱操作

      Boolean b2=new Boolean(b);

      Boolean b4=b1;//自动装箱

      System.out.println("......");

      System.out.println(b2);

      System.out.println(b4);

      //Boolean-->boolean  拆箱操作

      boolean b5=b2.booleanValue();//拆箱

      boolean b6=b2;//自动拆箱

//int 和  integer之间的相互转换

   //int-->Integer   装箱

   int i1=10;

   Integer i2=new Integer(i1);//装箱操作1

   Integer i3=i1;//自动装箱

   System.out.println(i1);

   System.out.println(i3);

   System.out.println(i2);

  

   //Integer-->int 拆箱

   int i4=i2.intValue();//拆箱

String:String类

常用的写法

String 类是一个字符类,也是我们常用的一个类,我们一般存储各种各样的字符数据

String str = "这是一个字符串";

除了这样的写法还有:

String str = new String("这是一个类该有的创建对象的方式");

实际上字符串在String内部是通过一个char[]数组表示的,因此,按下面的写法也是可以的:

String str = new String(new char[]{'这','是','一','种','创','建','方','式'});

常用的方法

1:char charAt(int index) 返回指定index处的char值

2:String concat(String str) 将指定的字符串str连接到当前字符串的末尾,并返回此新字符串对象

3:boolean contains(CharSequence s) 此字符串对象是否包含指定的字符序列(一般可以认为是字符串)

4:boolean startsWith(String prefix) 此字符串是否以指定的前缀开头

5:boolean endsWith(String suffix) 此字符串是否以指定的后缀结尾

6:boolean equals(Object anObject) 对比此字符串对象与指定对象的内容进行比对

7:boolean equalsIgnoreCase(String anotherString) 对比此字符串对象与指定内容进行忽略大小写的比对

8:int indexOf(int ch) 返回指定字符在字符串对象中首次出现的索引

9:int indexOf(String str) 返回指定字符串在字符串对象中首次出现的索引

10:int lastIndexOf(int ch) 返回指定字符在字符串对象中最后一次出现的索引

11:int lastIndexOf(String str) 返回指定字符串在字符串对象中最后一次出现的索引

12:int length() 返回此字符串的长度

13:boolean isEmpty() 此字符串是否为空串

14:String replace(char oldChar, char newChar) 返回使用newChar替换oldChar之后的新String对象

15:String replace(CharSequence oldString, CharSequence newString) 返回使用oldString替换newString之后的新String对象

16:String[] split(String regex) 按照给定的字符串或者正则表达式进行字符串的分割

18:String substring(int beginIndex) 返回指定起始位置的字符串

19:String substring(int beginIndex,int endIndex) 返回指定起始位置,指定结束位置的字符串

20:String toLowerCase() 返回此字符串的小写字符

21:String toUpperCase() 返回此字符串的大写字符

22:String trim() 返回去掉字符串前后所有空格的新字符串

23:static String valueOf(…) 将参数内容转换成String对象

java.util.Date

Date类是我们常用的一个日期和时间的对象,此类在 java.util 包中,并不是java.sql中的Date

 

date.getYear()+1900获取当前的年份后面必须加上1900(属于已淘汰的)

date.getMonth()+1获取当前的月份后面必须加上1(属于已淘汰的)

date.getDate()获取当前的日期(属于已淘汰的)

date.toString()转换为String类型

date.toGMTString()转换为GMT时区(属于已淘汰的)

date.toLocaleString()toLocaleString()(属于已淘汰的)

 

java.util.Calendar

日历类Calendar更多的是代表年 月 日 周 星期 上午 下午 夏时令等这样内容,Date用于记录某一个含日期的、精确到毫秒的时间。重点在代表一刹那的时间本身

 

java.util.Calendar

常用的方法

Calendar calendar = Calendar.getInstance();

Date date = calendar.getTime();

          System.out.println(date);

int year=calendar.get(Calendar.YEAR);

   System.out.println(year);//获取当前年份

   int month=calendar.get(Calendar.MONTH)+1;

   System.out.println(month);//获取当前月份

   int day=calendar.get(Calendar.DAY_OF_MONTH);

   System.out.println(day);//获取当前日期

   int hour=calendar.get(Calendar.HOUR_OF_DAY);

   System.out.println(hour);//获取当前小时

   int minute=calendar.get(Calendar.MINUTE);

   System.out.println(minute);//获取当前分钟

   int seconds=calendar.get(Calendar.SECOND);

   System.out.println(seconds);//获取当前秒

 

HOUR_OF_DAY是24小时制    hour是12小时制

java.text.SimpleDateFormat

格式化输出日期,此类提供了一些预定义字符串或者称之为占位符:

yyyy:年

MM:月

dd: 日

HH: 小时

mm: 分钟

ss: 秒

java.text.SimpleDateFormat

 

LocalDate类-本地日期类

 

LocalTime类-本地时间类

 

LocalDateTime类-本地日期时间类(输出的是ISO标准的一种格式  年月日T时间)

 

另外一种比较规范的用法:

 

获取年月日时分秒的用法(可以直接获取):

getMonth()返回英文    getMonthValue返回1---12  getHour是12小时制

 

根据指定的日期时间创建对象的用法:

 

根据标准格式创建对象的用法:

ISO 8601规定的日期和时间格式

 

 

DateTimeFormatter类

如果要自定义输出的格式,或者要把一个非ISO 8601格式的字符串解析成LocalDateTime,可以使用新的DateTimeFormatter:

 

按照指定的格式进行字符串日期的转换àLocalDateTime

LocalDateTime  dateTime=LocalDateTime.parse(“要转换的时间”,按照哪个格式走如dtf)

System类

System是一个类,这个System类主要是一些与系统相关的属性和方法的集合,而且其内部的方法全部是静态的,所以我们直接使用System直接调用就好

System.out.println("test");//输出

System.in;//输入

System.currentTimeMillis(); //获取当前时间的时间戳

System.gc();//垃圾回收

Scanner类

Scanner是一个基于正则表达式的文本扫描器~~可以从文件,输入流,字符串中解析出基本类型和字符串类型的值。Scanner类提供了多个构造器,不同的构造器可以接受文件,输入流,字符串作为数据源,用于从文件,输入流字符串中解析数据

Scanner 常用的一种形式:

 

Scanner类

常用的方法:

next()

 

 

nextLine()

 

next()

 

nextLine()

 

next()

1、一定要读取到有效字符后才可以结束输入。

2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。

3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。

4、next() 不能得到带有空格的字符串。

nextLine()

1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。

2、可以获得空白。

Math类

Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。

Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用

 

Math.PI是π

 

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

        System.out.println(Math.abs(10.1));     //10.1

 

       

        System.out.println(Math.ceil(-10.1));   //-10.0

        System.out.println(Math.ceil(10.7));    //11.0

        System.out.println(Math.ceil(-0.7));    //-0.0

        System.out.println(Math.ceil(0.0));     //0.0

        System.out.println(Math.ceil(-0.0));    //-0.0

        System.out.println(Math.ceil(-1.7));    //-1.0

 

       

        System.out.println(Math.floor(-10.1));  //-11.0

        System.out.println(Math.floor(10.7));   //10.0

        System.out.println(Math.floor(-0.7));   //-1.0

        System.out.println(Math.floor(0.0));    //0.0

        System.out.println(Math.floor(-0.0));   //-0.0

 

       

        System.out.println(Math.random());  //小于1大于0的double类型的数

        System.out.println(Math.random()*2);//大于0小于2的double类型的数

        System.out.println(Math.random()*2+1);//大于1小于3的double类型的数

 

       

        System.out.println(Math.rint(10.1));    //10.0

        System.out.println(Math.rint(10.7));    //11.0

        System.out.println(Math.rint(11.5));    //12.0

        System.out.println(Math.rint(10.5));    //10.0

        System.out.println(Math.rint(10.51));   //11.0

        System.out.println(Math.rint(-10.5));   //-10.0

        System.out.println(Math.rint(-11.5));   //-12.0

        System.out.println(Math.rint(-10.51));  //-11.0

        System.out.println(Math.rint(-10.6));   //-11.0

        System.out.println(Math.rint(-10.2));   //-10.0

 

       

        System.out.println(Math.round(10.1));   //10

        System.out.println(Math.round(10.7));   //11

        System.out.println(Math.round(10.5));   //11

        System.out.println(Math.round(10.51));  //11

        System.out.println(Math.round(-10.5));  //-10

        System.out.println(Math.round(-10.51)); //-11

        System.out.println(Math.round(-10.6));  //-11

        System.out.println(Math.round(-10.2));  //-10

Java中的Random类主要用来生成随机数

 

集合框架与泛型

list集合和set集合:添加元素的方法都是add
map集合:键值对存储数据,添加数据的方法是put

remove返回值是被删除的数据。

数组也可以看作是一种集合

 

数组有如下限制:

      数组初始化后大小不可变

      数组只能按索引顺序存取

集合有其特殊性:

集合存储的长度可变

集合只能存储对象(因为集合实际上存储的是对象的引用值也就是在堆中的地址,

所以不能存放基本数据类型数据,必须经过包装类才可以存储)

Java提供了两种类型的集合接口:

      Collection和Map,这两种都在java.util包下

      Collection是除Map外所有其他集合类的根接口

      Collection接口提供了2个子接口,List接口和Set接口

 

List接口

List接口继承自Collection接口,它规定实现它的类存储的是有序的、不唯一的集合元素,同时提供了具体的实现类ArrayList和linkedList

ArrayList:数组形式,连续的存储空间,查询速度快,增删速度慢

linkedList:双向链表,不连续的存储空间,增删速度快,查询速度慢

 

 

ArrayList类

ArrayList类实现了List接口,是基于动态数组的类,可存储null值

 

ArrayList 创建:

 

常用方法:
list集合可以重复数据,set不能重复
string长度是方法:length()
集合长度是方法:size()

boolean add(E e) 向集合中添加元素

E get(int index) 返回指定索引处的元素

int size() 获取此集合的长度

E remove(int index) 删除指定索引处的元素,并返回被删除的元素

boolean remove(E e) 删除集合中第一个出现的指定元素

boolean contains(Object o) list中是否包含某个元素,返回true或者false

E set(int index,E e) 根据索引将元素数值改变(替换)

int indexOf(E e) 返回在此集合中指定元素第一次出现的索引

int lastIndexOf(E e) 返回在此集合中指定元素最后一次出现的索引

boolean isEmpty() 判断集合是否为空

Object[] toArray() 将集合转换为数组

void clear() 将集合中的元素全部删除

ArrayList的两种创建方式:

List<存储的数据类型> nameList=new ArrayList<存储的数据类型>();

      ArrayList<存储的数据类型> nameList=new ArrayList<存储的数据类型>();

添加元素  例如:nameList.add(“张三”);

获取元素并输出  例如:String name=nameList.get(0);  System.out.println(name);

或者直接输出 System.out.println(nameList.get(0));

获取集合中元素的数量

      int count=nameList.size();

      System.out.println(count);或者直接输出

System.out.println(nameList.size());

其他类型同上

      //强制转换为此类型的数组

      Object[] nameArray=nameList.toArray();

      for (Object str : nameArray) {

            System.out.println(str);

      }

String[] nameArray=nameList.toArray(new String[0]);

      for (String str : nameArray) {

            System.out.println(str);

      }

清除集合中的元素:nameList.clear()   不能输出  是个void方法

ArrayList遍历集合中元素:

第一种遍历方式:普通for循环

 

第二种遍历方式:foreach循环

 

第三种遍历方式:迭代器

 

//获取集合中所有的元素-->遍历

            //1:使用普通for循环

            for (int i = 0; i < nameList.size(); i++) {

                  System.out.println(nameList.get(i));

            }

            //2:使用foreach循环

            for (String str: nameList) {

                  System.out.println(str);

            }

            //3:使用迭代器,需要导入Iterator的包

            System.out.println("---------");

            Iterator it=nameList.iterator();

            while (it.hasNext()) {

            System.out.println(it.next());

                 

            }

linkedList类

linkedList类实现了List接口,是基于双向链表的类,可存储null值

使用方法同上

linkedList创建方式:

 

linkedList常用方法:

void addFirst(E e) 将指定元素添加到此集合的开头

void addLast(E e) 将指定元素添加到此集合的末尾

E getFirst() 返回此集合的第一个元素

E getLast() 返回此集合的最后一个元素

E removeFirst() 删除此集合中的第一个元素

E removeLast() 删除此集合中的最后一个元素

linkedList遍历集合中元素:

第一种遍历方式:普通for循环

 

第二种遍历方式:foreach循环

 

第三种遍历方式:迭代器

Set接口

Set接口继承自Collection接口,它存储的是无序的、唯一的集合元素

HashSet类实现了Set接口,是基于HashMap实现,存储不重复、无序值

HashSet是根据对象的哈希值来确定元素在集合中的存储位置的,元素在插入时就确定了存储位置,因此,元素在集合中存储的位置是固定的(无序是指输出顺序与存储顺序不一致),具有良好的存储和查找性能(存储速度快)。保证元素的唯一性依赖于hashCode方法和equals方法。

HashSet创建方式:

 

//创建HashSet  HashSet<存储类型> set=new HashSet<存储类型>();

            HashSet set=new HashSet();

HashSet类

HashSet常用方法

boolean add(E e) 向集合中添加元素

int size() 获取此集合的长度

boolean remove(E e) 删除集合中第一个出现的指定元素

//添加元素  不能添加重复的内容

            set.add("陈亚琪");

            set.add("李传忠");

            set.add("张三");

            set.add("李四");

使用方法同上 没有get方法

//遍历集合中的元素

            //1:使用foreach

            for (String str : set) {

                  System.out.println(str);

            }

            //2:迭代器

            Iterator it=set.iterator();

            while (it.hasNext()) {

                  System.out.println(it.next());

                 

            }

 

HashSet遍历集合中元素:

第一种遍历方式:foreach循环

 

第二种遍历方式:迭代器

 

Map接口

Map接口是一个根接口,它存储的是键-值对(key-value),其中key不允许重复,value允许重复

HashMap类实现了Map接口,存储的是无序的键-值对(key-value)

    • 1.HashMap的key是用set集合来存放的,所以想做到 key不允许重复,key对应的类需要重写 hashCode和 equals 方法
    • 2.HashMap是线程不安全的
    • 3.HashMap中元素的位置是不定时更新的,即元素位置不是固定的

HashMap类

HashMap类创建方式

 

//创建HashMap

HashMap map=new HashMap();

      //添加数据

            map.put("name", "陈亚琪");

            //获取数据

            System.out.println(map.get("name"));

            //遍历HashMap中的所有内容  key   value

            //1:key  value  作为一个整体

            Iterator> it=map.entrySet().iterator();

while (it.hasNext()) {

      Map.Entry entry = (Map.Entry) it.next();

      String key=entry.getKey();

      String value=entry.getValue();

      System.out.println(key+":"+value);

     

}

//2:key作为set集合

Iterator it2=map.keySet().iterator();

while (it2.hasNext()) {

      String key=it2.next();

      String value=map.get(key);

      System.out.println(key+":"+value);

}

//3:value作为set集合

     Iterator it3=map.values().iterator();

     while(it3.hasNext()) {

     String value=it3.next();

     System.out.println(value);

     }

常用方法:

V put(K key,V value) 将指定的键与之关联的值存入HashMap中

V get(K key) 获取指定键所关联的值  获取的是key所对应的value值

V remove(K key) 删除

HashMap类遍历方式

第一种遍历方式 通过键值对Set集合遍历

 

第二种遍历方式 通过键Set集合遍历

 

 

第三种遍历方式 通过值Set集合遍历

 

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

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

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