== Java learning> array [array]

First, the definition of an array

/**
 * One-dimensional array definition
 *
 * Insert elements into an array
 * / 
Public  void Case1 () {
   // declare 
  int [] of arr1;

  // declare initialized + 
  int [] = arr2 is new new  int [. 3 ];
   // array assigned 
  arr2 is [0] =. 1 ;
  arr2[1] = 2;
  arr2[2] = 3;

  // declare + static initialization, arr3 and arr4 as 
  int [] = ARR3 new new  int [] {. 1, 2,. 3,. 4,. 5 };
   int [] = {arr4. 1, 2,. 3,. 4,. 5 };
}

Second, iterate

/**
 * One-dimensional array of access & traversal
 */
public void case2() {
  int[] arr = new int[]{1, 2, 3, 4, 5};

  // 普通for循环
  for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
  }

  // the foreach, enhanced for loop 
  for ( int NUM: ARR) {
    System.out.println(num);
  }

  // third way, jdk1.8 support that way 
  Arrays.stream (arr) .forEach (System.out :: println);
}

Third, the two-dimensional array

/**
 * Define two-dimensional array, initialization, access, traversal
 */
public void case3() {
  
  // declare 
  int [] [] arr1;

  // declare initialized + 
  int [] [] = arr2 is new new  int [. 3] [2 ];
  arr2[0][0] = 1;
  arr2[0][1] = 1;

  // declare + static initialization,. 3. 3 * 
  int [] [] = {{ARR3. 1, 2,. 3}, {. 4,. 5,. 6}, {. 7,. 8,. 9 }};
}

/**
 * Traverse the two-dimensional array
 * / 
Public  void case4 () {

  int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  // 第一种方式
  for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
      System.out.println(arr[i][j]);
    }
  }

  // second way 
  for ( int [] INTS: ARR) {
     for ( int I: INTS) {
      System.out.println(i);
    }
  }
}

IV Summary

definition

  • A plurality of data types by pressing the same order together to form the structure

Attributes

  • Name: Address stored in the memory array
  • Index: each element has a unique position in the array for positioning element
  • Element: each of the data array, i.e. the elements
  • Length: the number of elements in the array i.e.

Feature

  • Ordered the elements
  • Continuous memory space
  • Belonging to the reference data types

note

  • Once the specified length can not be modified i.e.

Fifth, practice

1, the definition of an array of integers, find the largest element

/**
 * Array Exercise: define an array of integers, find the largest element
 */
public void case1(){
  int[] arr = new int[10];
  for (int i=0;i<10;i++){
    arr[i] = i;
  }
  System.out.println(Arrays.toString(arr));
  int max = arr[0];
  for (int j=0;j<arr.length;j++){
    if (arr[j] > max){
      max = arr[j];
    }
  }
  System.out.println(max);
}

2, the definition of a two-dimensional array, and find all the elements

/**
 * Array Exercise: Defining a two-dimensional array, plus all the elements required and
 * / 
Public  void Case2 () {
   // declare and initialize a two-dimensional array 
  int [] [] = ARR new new  int [. 5] [. 3 ];
   // generates a two-dimensional array 
  for ( int I = 0; I <. 5 ; I ++ ) {
     for ( int J = 0; J <. 3; J ++ ) {
      arr[i][j] = i + j;
      System.out.print(arr[i][j]+" ");
    }
  }
  // Each loop element two-dimensional array and then summed 
  int SUM = 0 ;
   for ( int I = 0; I <arr.length; I ++ ) {
     for ( int J = 0; J <ARR [I] .length; ++ J ) {
      sum += arr[i][j];
    }
  }
  // wrap 
  System.out.println ();
  System.out.println(sum);
}

3, to the array location specified insertion / deletion of an element

/**
 * To insert a specified position array element
  */
int[] insertarray(int[] arr, int index, int value){

  int[] newArr = new int[arr.length + 1];
  for (int i = 0; i < arr.length; i++) {
    newArr[i] = arr[i];
  }
  for (int i = newArr.length - 1; i > index; i--) {
    newArr[i] = newArr[i - 1];
  }
  newArr[index] = value;
  arr = newArr;

  return arr;
}

/**
 * Delete the array element at the specified position
 */
int[] deletearray(int[] arr, int index){

  int[] newArr = new int[arr.length - 1];
  for (int i = 0; i <= arr.length - 1; i++){
    if (i < index){
      newArr[i] = arr[i];
    }
    else if (i > index){
      newArr[i-1] = arr[i];
    }
  }
  return newArr;
}

Guess you like

Origin www.cnblogs.com/L-Test/p/11350781.html