Home> Java> javaTutorial> body text

How to use java System class and Arrays class

WBOY
Release: 2023-05-22 20:52:24
forward
752 people have browsed it

1. Introduction

System is a system class. In the java.lang package of JDK, it can be seen that it is also a core language feature of java. The constructor of the System class is decorated with private and is not allowed to be instantiated. Therefore, the methods in the class are also static methods modified by static.

The Arrays class in JAVA is a tool class that implements array operations. It includes a variety of static methods that can implement array sorting and search, array comparison, and adding elements to the array. Functions such as copying and converting arrays to strings. These methods have overloaded methods for all basic types.

2. Detailed explanation of knowledge points

1. Concept

In the API, the introduction of the System class is relatively simple. We give the definition. System represents the system where the program is located and provides Corresponding system attribute information and system operations.

2. Commonly used methods

  • (1)public static void gc():Used to run the garbage collector in the JVM and complete the memory Cleaning up garbage

  • (2)public static void exit(int status): Used to end the running Java program. Just pass in a number as a parameter. Usually 0 is passed in as normal status, and others are abnormal status

  • (3)public static long currentTimeMillis():Get the current system time and January 1970 The millisecond difference between 00:00 on 01st

  • (4)public static Properties getProperties():is used to obtain the specified key (string name) System property information recorded in

Code demonstration:

package com.Test; import Test2.MyDate; import java.awt.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Properties; public class Main { private final static String name = "磊哥的java历险记-@51博客"; /* *public static void gc() //回收垃圾 *public static void exit(int status) //退出程序,0为正常状态,其他为异常状态 *public static long currentTimeMillis() //获取当前时间毫秒值 *public static Properties getProperties() //获取某个属性信息 */ public static void main(String[] args) { //构造方法被私有 不能创建对象 //System sy = new System(); //public static void exit(int status) //退出程序,0为正常状态,其他为异常状态 // System.out.println("我要退出了!"); // System.exit(0); // System.out.println("我已经退出了!"); //public static long currentTimeMillis() //获取当前时间毫秒值 long timeMillis = System.currentTimeMillis(); long time = new Date().getTime(); long timeInMillis = Calendar.getInstance().getTimeInMillis(); System.out.println(timeMillis); for(int i = 0; i < 5; i++) { System.out.println("i love java"); } long timeMillis2 = System.currentTimeMillis(); System.out.println(timeMillis2-timeMillis); //publicstatic Properties getProperties() //获取某个属性信息 Properties properties = System.getProperties(); System.out.println(properties); System.out.println("============="+name+"============="); } }
Copy after login

How to use java System class and Arrays class

3. Precautions

The System class cannot create objects manually because the construction method is privately modified, preventing the outside world from creating objects. All methods in the System class are static and can be accessed by class name. In the JDK, there are many such classes.

4. Arrays class

The Arrays class is a tool class provided by jdk specifically for operating arrays, located in the java.util package.

4.1. Commonly used methods of the Arrays class
  • (1) toString() method of Arrays - returns the string representation of the contents of a specified array..

  • (2) Arrays copyOf () //Copy the specified array, intercept or fill with null (if necessary), so that the copy has the specified length.

  • (3) Arrays sort() // Sort the specified type array in ascending numerical order.

  • (4) Arrays binarySearch () //Use binary search method to search the specified type array to obtain the specified value //Must be ordered

  • (5)Arrays fill() //Assign the specified type value to each element in the specified range of the specified type array

  • If two arrays of a given type mutually If equal, return true.

Code Demo:

package com.Test; import java.util.Arrays; /* Arrays toString () //返回指定数组内容的字符串表示形式。 Arrays copyOf () //复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。 Arrays sort() //对指定的类型数组按数字升序进行排序。 Arrays binarySearch () //使用二分搜索法来搜索制定类型数组,以获得指定的值 //必须有序 Arrays fill() //将指定的类型值分配给指定 类 型数组指定范围中的每个元素 Arrays equals() //如果两个指定的类型数组彼此相等,则返回 true。*/ public class Test{ private final static String name = "磊哥的java历险记-@51博客"; public static void main(String args[]){ //定义数组 int[] score={1,2,3}; int[] scores={1,2,3}; //数组之间比较,长度,值相等,则认为两个数组相等,返回布尔值 System.out.println("比较值和长度:"+Arrays.equals(score,scores)); //判断地址 if(score==scores){ System.out.println("score和scores比较,相等"); }else{ System.out.println("score和scores比较,不相等"); } //定义二维数组 int[][] sc={{222,333,1,2,0},{1,2,3,2,0}}; //排序 Arrays.sort(sc[1]); System.out.println("排序:"+Arrays.toString(sc[1])); System.out.println("按照下标取值:"+sc[0][1]+" "); //定义数据se int[] se={1,2,3,4,5}; //填充数组 Arrays.fill(se,0); System.out.println("填充:"+Arrays.toString(se)); //复制值到sx,增加指定长度 int[] sx=Arrays.copyOf(se,2); //输出sx的填充后的值 System.out.println("复制2:"+Arrays.toString(sx)); int[] xb={14,20,67,34,33,23,10}; //排序xb Arrays.sort(xb); System.out.println(Arrays.toString(xb)); //在排序后,通过二分查找,找到34的元素,并返回下标 int index1=Arrays.binarySearch(xb,34); System.out.println("二分法取值:"+index1); System.out.println("============="+name+"============="); } }
Copy after login

How to use java System class and Arrays class

How to use java System class and Arrays class

##4.2. Refinement Exercise
In using the Arrays class, we will use some basic algorithms such as sorting, etc.

Title:

  • (1) Create an int type array A, the value of A is {1,2,3,4,5}

  • (2) Copy the value of A into B with a length of 6

  • (3) Compare whether A and B are the same

Experimental steps:

  • (1) Declare a class Test and create two arrays

  • (2) Use Arrays related methods to complete the operation

Code demonstration:

package com.Test; import java.util.Arrays; /*声明一个类Test,并且创建两个数组*/ /* Arrays toString () //返回指定数组内容的字符串表示形式。 Arrays copyOf () //复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。 Arrays sort() //对指定的类型数组按数字升序进行排序。 Arrays binarySearch () //使用二分搜索法来搜索制定类型数组,以获得指定的值 //必须有序 Arrays fill() //将指定的类型值分配给指定 类 型数组指定范围中的每个元素 Arrays equals() //如果两个指定的类型数组彼此相等,则返回 true。*/ public class Main { private final static String name = "磊哥的java历险记-@51博客"; public static void main(String[] args){ //创建int类型数组A,A的值为{1,2,3,4,5} int[]A = new int[]{1,2,3,4,5}; //将A的值拷贝进长度为6的B中 int[]B = Arrays.copyOf(A, 6); //比较A和B是否相同 System.out.println("两个数组是否相等:"+Arrays.equals(A, B)); System.out.println("============="+name+"============="); } }
Copy after login

How to use java System class and Arrays class

The above is the detailed content of How to use java System class and Arrays class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!