Java Basics (4)-Arrays (1)

Source: Internet
Author: User
Tags array length

Arrays : Containers that store multiple elements of the same data type
Array Initialization :


    • int [] arr = new Int[5]

    • int [] arr = new int [] {1,2,3,4,5}
      int [] arr = {1,2,3,4,5}

Memory allocation
Stack memory: Used to store local variables, and when the data is exhausted, the occupied space is automatically freed. Some basic types of variables and object reference variables are allocated in the function stack memory
Heap Memory: For storing objects and arrays created by new
Method Area:

Heap Memory Features:
Every new thing comes with an address value.
Each variable has a default value

Common Array Operations

    • Gets the elements in the array
classarraydemo3{ Public Static voidMain (string[] args) {//int[] arr = new int[3];        int[] arr = {1,5,7,4,6,8};  for(intI=0; i<arr.length;i++) {System. out. println ("arr["+i+"]="+arr[i]+";"); }    }}/*arr.lenth that returns the length of the array arr*/

    • Sum of array elements
classarraysum{ Public Static voidMain (string[] args) {int[] arr =New int[]{1,2,3,4,5,6,7,8,9,Ten}; intsum =0;  for(intI=0; i<arr.length;i++) {sum+=Arr[i]; } System. out. println ("sum="+sum); }}

    • Iterating through the elements in an array
 Public classarraydemo4{ Public Static voidMain (string[] args) {int[] arr =New int[]{1,2, the,4};//PrintArray (arr);//can be called directly or Arraydemo4.printarray (arr);printArray1 (arr); }             Public Static voidPrintArray (int[] arr) {//Note that this is a static method         for(inti =0; i<arr.length;i++) {System. out. println (Arr[i]); }    }         Public Static voidPrintArray1 (int[] arr) {         for(inti =0; i<arr.length;i++){            if(i==arr.length-1){//If this is the last elementSystem. out. println (Arr[i]); }Else{//if not the last elementSystem. out. Print (arr[i]+","); }        }        }}/*running Result: 1,2,34,4*/

    • Find the maximum and minimum values
classarraytest1{ Public Static voidMain (string[] args) {int[] arr =New int[]{7,2,6,2};//define an array/*int max = arr[0];            for (int i = 1; i<arr.length;i++) {if (Arr[i]>max) {max = arr[i]; }} System.out.println (max);*/        intmax = Getmax (arr);//static methods can be called directly//System.out.println (max);        intMin =getmin (arr); System. out. println (min); }             Public Static intGetmax (int[] arr) {        //find any element in the array as a reference        intmax = arr[0]; //traversing other elements         for(inti =1; i<arr.length;i++){            //get in turn and compare with the reference, if the big left, the small wine left            if(arr[i]>arr[0]) {max=Arr[i]; }        }        //The maximum value that is stored in the reference        returnMax; }     Public Static intGetmin (int[] arr) {        intMin = arr[0];  for(inti =1; i<arr.length;i++){            if(arr[i]<min) {min=Arr[i]; }        }        returnmin; }}

    • Array inversion
classarraydemo{ Public Static voidMain (string[] args) {int[] arr =New int[]{ the, +, $,543, the}; System. out. Print ("Original array:");                PrintArray (arr); System. out. Print ("Inverted array:");    Reversearray (arr); }    //iterating through an array     Public Static voidPrintArray (int[] arr) {         for(inti =0; i<arr.length;i++){//System.out.println (Arr[i]);//output Each element, one element at a line            if(i = = arr.length-1) {System. out. println (Arr[i]); }Else{System. out. Print (arr[i]+","); }        }    }    //invert an array     Public Static voidReversearray (int[] arr) {         for(inti =0; i<arr.length/2; i++){            inttemp =Arr[i]; Arr[i]= arr[arr.length-1-H]; Arr[arr.length-1-I.] =temp; } printArray (arr);//This calls the print array method, cannot print arr directly, this is the address value    }}/*Original array: 112,32,45,543,76 inverted array: 76,543,45,32,112*/

    • Select sort
classarraytest2{//Sorting Methods     Public Static voidSelectsort (int[] arr) {         for(intx=0; x<arr.length;x++){             for(inty=x+1; y<arr.length-1; y++){                if(arr[x]>Arr[y]) {                    inttemp =Arr[x]; ARR[X]=Arr[y]; Arr[y]=temp; }            }        }    }    //Main Method     Public Static voidMain (string[] args) {int[] arr = {3,4,2,8,6,9, A};        PrintArray (arr);        Selectsort (arr);    PrintArray (arr); }    //Print Array Method     Public Static voidPrintArray (int[] arr) {System. out. Print ("[");  for(intx=0; x<arr.length;x++){            if(x!=arr.length-1) {System. out. Print (arr[x]+","); }Else{System. out. println (arr[x]+"]"); }        }    }}

Java Basics (4)-Arrays (1)

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.