javaSE-- V. array

1. Why do we need an array

  • Open space concept variables in memory for storing data of a fixed data type
  • Open space array in memory for storing a series of the same data type

2, using an array of basic grammar

Declared type [] array name;

Space allocation array name = new type [length];

Assignment array name [index] = data value; index from 0 to one less than the number of elements in the array

Using the array name [index];

3, the other using array syntax

 Type [] array name list = {value}; statement given initial distribution space

Type [] = new Array type name [] {} list of values;

  • Type [] array name;
  • Type name = new Array [] {} list of values;

4, an array of ten operations

  • New

Assigning a single element array name [index] = value;

All the elements of an array assignment

int[] nums=new int[5];

for(int i=0;i<nums.length;i++){

nums[i]=??;

}

 

 

 

  • Additional new style

// Create an array of original

int[] nums={1,2,3,4};

// original array in a new 10

// expansion of operations

// original array backup

int[] tNums=nums;

Operation for expansion of the original array // way to force open space

nums=new int[nums.length+1];

// restore data to back up data stored in the array back to the original array expansion after

for (int i = 0; i < tNums.length; i++) {

the nums [ i ] = tNums [ i ]; // element value in the backup array subscripts i stored in the array corresponds to a position corresponding to the expansion index

}

// add data

    nums[nums.length-1]=10;

 

  • modify

To find the next indexing position, then the replace operation

  • delete

First find out the subscript position, backup, reset the original array

  • Single check marking

int flag = -1; // flag is not found to find the elements for preservation if it finds an element found at the location where the mark

for(int i=0;i<nums.length;i++){

    if(nums[i]==num){

        flag=i;

        break;

    }

}

if(flag==-1){

     //did not find

}else{

     // find the cursor position in the flag

}

  • Full investigation

int[] nums={1,2,3,4,5};

for(int i=0;i<nums.length;i++){

System.out.println(nums[i]);

}

 

  • Seeking maximum

A first set of maximum variable (array subscript 0) is stored, recycled

  • Minimum requirements
  • Averaging
  • Seek sum
  • Sequence

5, reference types and value type

1) the value stored in the value type variable spatial reference type variable memory address space

2) the value of the type comprising

Plastic byte short int long

Float float double

Character char

Boolean boolean

Enumeration enum

3) In addition to the large value for all 9 types of reference types are

String String

An array of any type of array

The system defined classes like Scanner Arrays Random

Custom class Test1 Test2 Test3 ..

Note: null is a reference type-specific defaults

4) When the value of type reference character string type and value assignment change operation differences

  • Relationship between the value of two variables no longer any variable value type operations after completion of the assignment does not affect the other variable is changed

int  num= 10;

int  num1=a;

a = 20;

System.out.println(num1);// 20

  •   Reference types of data replication is the address assignment operation two variables have the same point to the same memory address space value either make changes to data while the other will be affected

int[] nums={10};

int[] nums1=nums;

nums[0]=20;

System.out.println(nums1[0]);

  • However, the string belonging to a reference type of string pool with no changes when the string variable reassigning not change the data storage space but changes the value of the address stored in the variable itself exhibits an effect similar to the value of the type

String str1="abc";

String str2=str1;

str1="def";

System.out.println(str2);

  • When value types are passed as parameters, a value is passed, all modifications made in the method without affecting the original value of the variable.
  • When data is passed as a parameter reference type, the address is passed, the process carried out in any changes will affect the original data.
  • Although the string is a reference type, but with nature can not be changed.

 

Guess you like

Origin blog.csdn.net/weixin_43619912/article/details/94725079