Java basics-day 4

Source: Internet
Author: User

I. Definition of arrays new operators generate container entities in memory 1. Concept: a set of data of the same type. In fact, an array is a container. 2. array benefits: the array elements can be automatically numbered from 0 to facilitate operations on these elements. 3. arrays are arrays and reference data types. The preceding format type is the data type in the container. 4. Format 1: element type [] array name = new element type [number of elements or array length]. Int [] arr = new int [5]; 5. Format 2: Element type [] array name = new element type [] {element, element, element} int [] arr = new int [] {3, 4, 5, 6} // static initialization int [] arr = {3, 4, 5, 6} int arr [] = {3, 4, 5, 6} // This is also true, but it is not recommended to write this statement 6, example (1), int [] x = new int [3]; int [] y = x; y [1] = 1; x [1] is also 1, reference type (2), int [] x = new int [3]; int [] y = new int [3]; y [1] = 1; then x [1] is still 0, two Memory Spaces (3), int [] x = new int [3]; System. out. print (x [5]); no error will be reported during compilation, because it is only compiled, but the memory space will be allocated during runtime. When x [5] is used, an exception jav will be reported. A. lang. when the ArrayIndexOutOfBoundsException array has an out-of-bounds exception operation, it accesses the non-existent badge (4), int [] x = new int [3]; x = null; // at this time, x is assigned null and no longer points to the array, not the array System. out. print (x [5]); java. lang. when the NullpointerException null pointer runs abnormally, a null pointer exception is reported: when the reference does not point to a null value, the reference is still used to operate the object. 7. An attribute in the array operation array can directly obtain the number of array elements :. length usage: array name. length (1), get the element int [] arr = new int [3]; arr [1]; (2) traverse int [] arr = new int [3]; for (int x = 0; x <3; x ++) {System. out. println (arr [x]);} (3), length traversal int [] arr = new int [3]; for (int x = 0; x <x. length; x ++) {System. out. println (arr [x]);} (4), direct print (array) will print the [1 @ hexadecimal [: indicates the array, 1: indicates the type ,@: followed by address (5), get the maximum value of public class Demo {public static void main (String args []) {int [] num = new Int [] {,}; int max = theFunction (num); System. out. println ("max number is:" max);} public static int theFunction (int [] num) {int max = num [0]; for (int x = 0; x <num. length; x ++) {max = max <num [x]? Num [x]: max;} return max;} (6), array selection sorting method public static void theFunction (int [] num) {int temp = 0; for (int x = 0; x <num. length-1; x ++) {for (int y = x + 1; y <num. length; y ++) {if (num [x]> num [y]) {temp = num [x]; num [x] = num [y]; num [y] = temp ;}}( 7), Bubble Sorting: compare two adjacent elements. If the conditions are met, the transposition is low // efficiency and the heap transposition is slow, hill sorting fast public static void theFunction (int [] num) {int temp = 0; for (int x = 0; x <num. length-1; x ++) {for (int y = 0; y <num. length-x-1; y ++) {If (num [y]> num [y + 1]) {temp = num [y + 1]; num [y + 1] = num [y]; num [y] = temp ;}}} (8), java comes with sorting import java. util. *; // introduce Arrays. sort (num); (9). Search for arrays // return the badge.-1 is not returned. The conventional half-lookup method is only applicable to ordered arrays (1) the first public static int halfSearch (int [] arr, int key) {int min, max, mid; min = 0; max = arr. length-1; mid = (min + max)/2; while (arr [mid]! = Key) {if (key> arr [mid]) {min = mid + 1;} else if (key <arr [mid]) {min = max-1 ;} if (min> max) {return-1;} mid = (min + max)/2;} return mid;} (2) public static int halfSearch (int [] arr, int key) {int min, max, mid; min = 0; max = arr. length-1; while (min <= max) {mid = (min + max)> 1; if (key> arr [mid]) {min = mid + 1 ;} else if (key <arr [mid]) {max = mid-1 ;}else {return mid ;}} return-1 ;}8. hexadecimal conversion (1), decimal --- "binary publi C static void toBin (int num) {StringBuffer sb = new StringBuffer (); while (num> 0) {sb. append (num % 2); num = num> 1;} System. out. print (sb. reverse ();} (2), decimal ----- hexadecimal public static void toBin (int num) {StringBuffer sb = new StringBuffer (); for (int x = 0; x <8; x ++) {int temp = num & 15; if (temp> 9) {sb. append (char) (temp-10 + 'A');} else {sb. append (temp);} num = num >>> 4;} System. out. print (sb. reverse ();} (3), conversion optimization, can Decimal to binary, decimal, octal public static void trans (int num, int base, int offset) {if (num = 0) {System. out. print ('0'); return;} char [] chs = {'0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E ', 'F'}; char [] arr = new char [32]; int pos = arr. length; while (num! = 0) {int temp = num & base; arr [-- pos] = chs [temp]; num = num >>> offset ;}for (int x = pos; x <arr. length; x ++) {System. out. print (arr [x]);} 8. Two-dimensional array (1). Format: int [] [] arr = new int [3] [2]; three two-dimensional arrays with the name of arr are defined. Each array has two elements. The names of one-dimensional arrays are given by arr [0] arr [1] arr [2] in the first one-dimensional array, the expression for assigning a value of 78 to the 1st position is as follows: arr [0] [1] = 78; (2), Format 2: int [] [] arr = new int [3] []; two-dimensional arrays have three one-dimensional arrays. Each one-dimensional array has the default initialization value null. You can initialize these three one-dimensional arrays respectively. arr [0] = new int [3]; arr [1] = new int [2]; arr [2] = new int [1]; (3), int [] [] arr = new int [3] [2]; 3 required, 2 not allowed to write (4), Format 3: int [] [] arr = {1, 2, 3}, {4}, {5}, {3, 4 },};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.