Arrays in Java

Source: Internet
Author: User

(a) Array: 1) The length and type of the array is fixed 2) several major elements: int[] arr = new int[5]; subscript (subscript of array starting from 0) element: arr[i] i>=0&&i<5 type, here is int type length, View the array length arr.length, here is 52) two basic concepts in the array: Stack-heap 1, essential difference: the stack in the array does not initialize by default: int n; will be error int n = 0; The system is not initialized by the heap in the compiled array until the variable is initialized:
Int 0
Float 0.0
Char Null character
Boolean False
Non-basic data types Null
&NBSP;2, specific performance: Local variables are stored in the stack   3, array initialization: 1) int[] arr = new Int[5];arr[0] = 1;arr[1] = 2; ... 2) int[] score = new int[]{80,60,90}//Create array with assignment can be done in the same statement int[] score = {80,60,90}3) for (int i = 0; i < arr.length; i++) {AR R[i] = (int) (Math.random () *10);//The Random method in the Math class is called here, and a type conversion} 4, two-dimensional array: a two-dimensional array is essentially a one-dimensional array, just one more dimension ; You can interpret a two-dimensional array as a new array of multiple one-dimensional arrays. 1) expression: int[][] sc = new Int[4][5]; sc.length = = 4; indicates that the two-dimensional array contains 4 one-dimensional arrays, that is, the length of the two-dimensional array is 4 for (int i = 0; i < sc.length; i + +) {int[] tep = Sc[i];} Then tep.length = = 5; This indicates that each one-dimensional array in the two-dimensional array has a number of elements corresponding to 5 2) stored: int[][] s = new int[3][2]; Example: int[][] arr = {{1,2,3},{4,5},{6,7,8,9 }}; The:  stack will only hold the array in memory and will not hold the contents of the data, the element data of the array is stored in the heap  5, array operations: Sort: bubbling sort: double[] arr = new Double[5];for (int i = 0; i < Arr.length; i++) {Arr[i] = sc.nextdouble ();}  for (int i = 0; i < arr.length-1; i++) {for (int j = 0; j< arr.length-2; j + +) {if (Arr[j] > Arr[j+1])    {Double temp = arr[j];    ARR[J] = arr[j+1];    Arr[j + 1] = temp; }}}  

Arrays in Java

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.