java common API array bubble sorting, Arrays tool class, wrapper class, Date,SimpleFormat related

Keywords: Java

1.1 bubble sorting of arrays

Bubble sort: it is to compare two adjacent elements and arrange the elements with large element values backward in turn

1.1.1 bubble sorting principle diagram of array sorting

1.2 Arrays tools

1.2.1 overview and use of arrays

 Arrays:Provides various methods for manipulating arrays.
public static String toString(int[] a):Convert array to string
public static void sort(int[] a):Sort the array in ascending order

Here are some inline snippets.

package com.itheima;

import java.util.Arrays;

/*
 * Arrays:Provides various methods for manipulating arrays.
 * public static String toString(int[] a):Convert array to string
 * public static void sort(int[] a):Sort the array in ascending order
 */
public class ArraysDemo {
	public static void main(String[] args) {
		//Define an array
		int[] arr = {24,69,80,57,13};
		
		//public static String toString(int[] a): convert an array into a string
		System.out.println("Before sorting:"+Arrays.toString(arr));
		
		//public static void sort(int[] a): sort the array in ascending order
		Arrays.sort(arr);
		
		System.out.println("After sorting:"+Arrays.toString(arr));
	}
}

1.2.2 problems of construction methods in arrays class

Is there really no constructor in the Arrays class?
If there is no constructor in a class, the system will provide a parameterless constructor.
But we don't see the construction method of the Arrays class in the help document. Why?

There are constructors in the Arrays class, but the constructors are modified by private and cannot be used by the outside world. Because the outside world can't use it, you can't see it in the help document.
By looking at the source code, we found the following:
private Arrays() {}
The design of Arrays class is the design idea of common tool classes:
Constructor private.
Members are decorated with static.
Math, Collections, etc

2.1 overview of basic packaging types

Requirement: do I want to judge whether a data is within the int range?
To judge whether a data is within the int range, we first need to know the int range. We talked about the basic data types earlier:
-2147483648 to 2147483647
In order to perform more and more convenient operations on basic data types, Java provides a corresponding reference type for each basic data type.

Basic type packing class:
Byte byte
Short short
Integer int
Long long
Float float
Double double
Character char
Boolean boolean

The most common use of basic data type wrapper classes is to convert between and strings.

2.2 overview and construction method of integer class

Integer: the integer class wraps a value of the basic type int in an object.
Construction method:
Integer(int value)
Integer(String s)
Note: this string must consist of numeric characters

2.3 conversion between int type and String type

Conversion between int type and String type

  • int – String
    String class: public static String valueOf(int i)
  • String – int
    Integer class: public static int parseInt(String s)

Here are some inline snippets.

package com.itheima_03;
/*
 * int Conversion between type and String type
 * 
 * int	--	String
 * 		String Class: public static String valueOf(int i)
 * 
 * String	--	int
 * 		Integer Class: public static int parseInt(String s)
 */
public class IntegerDemo {
	public static void main(String[] args) {
		//int	--	String
		int number = 100;
		//Mode 1
		String s1 = "" + number;
		System.out.println(s1);
		//Mode 2
		//public static String valueOf(int i)
		String s2 = String.valueOf(number);
		System.out.println(s2);
		System.out.println("--------------");
		
		//String  -- int
		String s = "100";
		//Mode 1
		//String -- Integer -- int
		Integer i = new Integer(s);
		//public int intValue()
		int x = i.intValue();
		System.out.println(x);
		//Mode 2
		//public static int parseInt(String s)
		int y = Integer.parseInt(s);
		System.out.println(y);
		
	}
}

3.1 overview and construction method of date class

Date:Date represents a specific moment, accurate to milliseconds.
Construction method:
Date(): a date object created based on the current time
Date(long date): creates an object based on the given millisecond value, starting from 00:00:00 on January 1, 1970

3.2 member methods getTime() and setTime() of date class

Member method:
public long getTime(): gets the millisecond value. From 00:00:00 on January 1, 1970.
public void setTime(long time): sets the time. The value given is milliseconds.
Get a millisecond value from Date:
getTime()
From a millisecond value to a Date object
Construction method
setTime(long time)

3.3 overview and use of simpledateformat class

SimpleDateFormat: is a concrete class that formats and parses dates in a locale dependent manner.
It allows formatting (date - > text), parsing (text - > date)

Format (Date - > text): Date – String
public final String format(Date date)

Parse (text - > Date): String – Date
public Date parse(String source)
package com.itheima_01;

Here are some inline snippets.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * SimpleDateFormat:Is a concrete class that formats and parses dates in a locale dependent manner.
 * It allows formatting (date - > text), parsing (text - > date)
 * 
 * Format (date - > text): Date -- String
 * 		public final String format(Date date)
 * 
 * Parse (text - > date): String -- Date
 * 		public Date parse(String source)
 */
public class SimpleDateFormatDemo {
	public static void main(String[] args) throws ParseException {
		/*
		//Date -- String
		Date d = new Date();
		//SimpleDateFormat(): Use default mode
		//SimpleDateFormat sdf = new SimpleDateFormat();
		//SimpleDateFormat(String pattern):Use the given pattern
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd, HH:mm:ss ");
		String s = sdf.format(d);
		//Now, although the date is formatted into a string, it is not the format we want
		//The format we want is xx:xx:xx: xx
		System.out.println(s);
		*/
		
		//String -- Date
		String str = "2080-08-08 12:23:45";
		//When parsing a string into a date, please note that the format of the pattern string and the given date string should match
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sdf.parse(str);
		System.out.println(d);
	}
}

Posted by LAX on Sat, 16 Oct 2021 21:56:45 -0700