Java basics 5: java Basics

Source: Internet
Author: User

Java basics 5: java Basics
1. array Definition

  • Concept: An array is a container for a collection of data of the same type.
  • Array advantages: You can automatically number the elements in the array from 0 to facilitate operations on these elements.
  • Format 1:
    • Element type [] array name = new element type [number of elements or array length];
  • Format 2:
    • Element type [] array name = new element type [] {element 1, element 2 ,......};

 

  • Example:
Package java005;/***** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [5] ;}}
  • Example:
Package java005;/***** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [] {};}}
  • Example:
Package java005;/***** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [] {1, 2, 3 };}}
  • Example:
Package java005;/**** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [5]; system. out. println (arr [0]); arr [1] = 59; for (int x = 0; x <arr. length; x ++) {System. out. println (arr [x]) ;}}

 

2. Memory Allocation and features of Arrays
  • Memory Division:
    • Register
    • Local Method Area
    • Method Area
    • Heap memory
    • Stack memory

 

  • Stack memory: local variables are stored, and the variables are automatically released once the scope of the variables ends.

 

  • Heap memory: storage arrays and objects (in fact, arrays are objects ).
    • Each object has a first address value.
    • Each variable in heap memory has a default initialization value, which varies with the type. The integer is 0, the decimal point is 0.0, The boolean type is false, and the char type is '\ u0000 '.
    • Garbage collection mechanism.

 

  • Illustration: int [] arr = new int [5];
    • ① Stack the main method

    • ② The main method enters the stack and executes the code in the main method. When it is executed to the new int [5];, a continuous space is opened in the heap memory, with a length of 5, initialize the default value of the array object in the heap as the default value of the corresponding type, and allocate the memory address of the array object in the heap.

    • ③ When the address in the heap is assigned to the arr variable on the left, the arr points to the array object in the heap.

 

3. Array Operation FAQs
  • ArrayIndexOutOfBoundsException
Package java005;/**** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [5]; system. out. println (arr [5]);}

 

  • NullPointerException
Package java005;/**** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [5]; system. out. println (arr [arr. length-1]); arr = null; System. out. println (arr [arr. length-1]) ;}}

 

4 array Common Operations 4.1 traverse Arrays
Package java005;/***** Note: array */public class ArrayDemo {public static void main (String [] args) {int [] arr = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; for (int x = 0; x <arr. length; x ++) {System. out. print (arr [x] + "\ t ");}}}
4.2 max value
Package java005;/***** Note: Evaluate the maximum value */public class ArrayDemo2 {public static void main (String [] args) {int [] arr = new int [] {1, 11, 33,-1,-10}; int max = max (arr); int min = min (arr); System. out. println ("maximum value:" + max); System. out. println ("minimum value:" + min);}/*** calculate the maximum value of an array * @ param arr * @ return */public static int max (int [] arr) {if (arr. length = 0) {throw new RuntimeException ("the array cannot be blank");} int max = arr [0]; for (int x = 0; x <arr. length; x ++) {if (max <arr [x]) {max = arr [x] ;}} return max ;} /*** calculate the minimum value of an array * @ param arr * @ return */public static int min (int [] arr) {if (arr. length = 0) {throw new RuntimeException ("the array cannot be blank");} int min = arr [0]; for (int x = 0; x <arr. length; x ++) {if (min> arr [x]) {min = arr [x] ;}} return min ;}}
4.3 select sorting

 

 

Package java005;/*** optional /9/3 * Description: Select sort */public class ArraySortDemo {public static void main (String [] args) {int [] arr = {34,19, 11,103, 56}; // printArray (arr) Before sorting; // sortArray (arr) after sorting; // printArray (arr) after sorting );} /*** print array * @ param arr */public static void printArray (int [] arr) {System. out. print ("["); for (int x = 0; x <arr. length; x ++) {if (x = (arr. length-1) {System. out. print (arr [x] + "]");} else {System. out. print (arr [x] + ",") ;}}/ *** sorts arrays * @ param arr */public static void sortArray (int [] arr) {for (int x = 0; x <arr. length-1; x ++) {for (int y = x + 1; y <arr. length; y ++) {if (arr [x]> arr [y]) {int temp = arr [y]; arr [y] = arr [x]; arr [x] = temp ;}}}}}

4.4 Bubble Sorting

 

 

Package java005;/***** Note: Bubble Sorting */public class ArraySortDemo2 {public static void main (String [] args) {int [] arr = new int [] {11,109,}; // pre-sorting printArray (arr); sortArray (arr ); // printArray (arr) after sorting;}/*** each element in the output array */public static void printArray (int [] arr) {System. out. print ("["); for (int x = 0; x <arr. length; x ++) {if (x = arr. length-1) {System. out. println (arr [x] + "]");} else {System. out. print (arr [x] + ",") ;}}/ *** Bubble Sorting Algorithm * @ param arr */public static void sortArray (int [] arr) {for (int x = 0; x <arr. length-1; x ++) {for (int y = 0; y <arr. length-1-x; y ++) {if (arr [y]> arr [y + 1]) {int temp = arr [y]; arr [y] = arr [y + 1]; arr [y + 1] = temp ;}}}}}
4.5 find the location of the specified value in the array 4.5.1 normal lookup Method
Package java005;/***** Note: Bubble Sorting */public class ArraySortDemo2 {public static void main (String [] args) {int [] arr = new int [] {11,109, 109,}; int index = commonFindElementIndexByValue (arr,); System. out. println ("index value:" + index);} public static int commonFindElementIndexByValue (int [] arr, int value) {if (arr = null | arr. length = 0) {return-1;} for (int x = 0; x <arr. length; x ++) {if (value = arr [x]) {return x ;}} return-1 ;}}
4.5.2 binary search (half-fold search) -- Sort first
Package java005;/***** release /9/4 * Description: Binary Search Method */public class BinarySearchDemo {public static void main (String [] args) {int [] arr = new int [] {11,109,}; sort (arr); int index = binarySearch (arr, 56); System. out. print ("index:" + index);}/*** sort * @ param arr */public static void sort (int [] arr) {for (int x = 0; x <arr. length-1; x ++) {for (int y = 0; y <arr. length-1-x; y ++) {if (arr [y]> arr [y + 1]) {I Nt temp = arr [y]; arr [y] = arr [y + 1]; arr [y + 1] = temp ;}}}} /*** Binary Search Method * @ param arr * @ param value * @ return */public static int binarySearch (int [] arr, int value) {if (arr = null | arr. length = 0) {return-1;} int min = 0; int max = arr. length-1; int mid = (min + max)/2; while (arr [mid]! = Value) {if (value> arr [mid]) {min = mid + 1;} else if (value <arr [mid]) {max = mid-1 ;} if (min> max) {return-1;} mid = (min + max)/2;} return mid ;}}
  • The binary search method has some disadvantages. If the current array is not sorted, the returned index is not the index value before sorting.
Package java005; import javax. lang. model. element. variableElement;/**** Description: Binary Search Method */public class BinarySearchDemo {public static void main (String [] args) {int [] arr = new int [] {11,109,}; sort (arr); int index = binarySearch (arr, 56); System. out. print ("index:" + index);}/*** sort * @ param arr */public static void sort (int [] arr) {for (int x = 0; x <arr. length-1; x ++) {for (int y = 0; y <arr. length-1-x; y ++) {if (arr [y]> arr [y + 1]) {int temp = arr [y]; arr [y] = arr [y + 1]; arr [y + 1] = temp ;}}}} /*** Binary Search Method * @ param arr * @ param value * @ return */public static int binarySearch (int [] arr, int value) {if (arr = null | arr. length = 0) {return-1;} int min = 0; int max = arr. length-1; int mid = 0; while (min <max) {mid = (min + max)> 1; if (value> arr [mid]) {min = mid + 1;} else if (value <arr [mid]) {max = mid-1;} else {return mid ;}} return-1 ;}}

 

 

 

 

 

5. Array (two-dimensional array)

 

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.