How to use java System class and Arrays class
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+"============="); } }
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+"============="); } }
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+"============="); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
