Array&Sparse Array

 

Array

  1. How to create an array?

    // method 1
    //dataType[] name = value
    int[] nums;// declare an array
    // method 2
    //dataType name[] = value
    int nums[];
    1. Static initialization

      int[] a = {1,2,3}
    1. Dynamic initialization

      int[] a;
      a = new int[3];
      a[0] = 1;

  1. How to use an array?

  • Array in for loop:

int[] nums;//1. declare an array
nums = new int[3];//2. create an array
nums[0] = 1; //3. assign a value
nums[1] = 2;
nums[2] = 3;
System.output.println(nums[0]);//4.print a value
// 1. print all of elements
for(int i = 0; i<nums.length;i++){
 System.output.println(nums[i]);
}
// 2. calculate the sum of an array
int sum = 0
for(int i = 0; i<nums.length;i++){
 sum+=nums[i];
}
//3. fint the max element of array
int max = nums[0];
for(int i = 0; i<nums.length;i++){
 if(array[i]>max){
   max = array[i];
}
}
  • Array in for each loop:

for(int num: nums){
 System.output.println(num)
}
  • Invert an array:

public static int[] invert(int[] array){
 int[] invert = new int[array.length];
 for (int i = 0,j = invert.length-1; i array.length;i++,j--) {
           invert[i] = array[j];
      }
       return invert;
  }
  1. Memory analysis

    1. Java memory

      1. heap to store new object and array

        nums = new int[3];//2. create an array
        nums[0] = 1; //3. assign a value
        nums[1] = 2;
        nums[2] = 3;

      2. stack to store variable type

        int[] nums;
  2. Sparse Array - to save memory


Sparse array

  1. To save memory

The following code illustrate how program transfer an original 2D array to a sparse array, and convert sparse array to original array by sparse array we generated as well

package Array;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.lang.reflect.Array;
import java.util.Arrays;

public class SparseArray {
   public static void main(String[] args) {
       int[][] checkerboard = new int[11][11];
       checkerboard[1][2]=1;
       checkerboard[10][10]=2;
       System.out.println("Original checkerboard");
       Print2DArray(checkerboard);
       //get amount of value that is not 0
       int sum = 0;
       for (int i = 0; i < checkerboard.length; i++) {
           for (int j = 0; j < checkerboard.length; j++) {
               if (checkerboard[i][j]!=0){
                   sum+=1;
              }
          }
      }
       System.out.println("the number of none 0 element:"+sum);
       // create a sparse array
       int[][] SpaseArray = new int[sum+1][3];
       SpaseArray[0][0] = checkerboard.length;
       SpaseArray[0][1] = checkerboard.length;
       SpaseArray[0][2] = sum;
//       System.out.println(Arrays.toString(SpaseArray));
       int count = 0;
       for (int i = 0; i < checkerboard.length; i++) {
           for (int j = 0; j < checkerboard[i].length; j++) {
               if (checkerboard[i][j]!=0){
                   count++;
                   SpaseArray[count][0] = i;
                   SpaseArray[count][1] = j;
                   SpaseArray[count][2] = checkerboard[i][j];
              }
          }
      }
       System.out.println("Sparse Array");
       Print2DArray(SpaseArray);
       System.out.println("===================================");
       System.out.println("From Sparse Array to original Array");
       int[][] array = new int[SpaseArray[0][0]][SpaseArray[0][1]];
       for (int i = 1; i < SpaseArray.length; i++) {
           array[SpaseArray[i][0]][SpaseArray[i][1]] = SpaseArray[i][2];
      }
       
       Print2DArray(array);
  }
   //function Print2DArray
   public static void Print2DArray(int[][] array){
       for (int[] ints : array) {
           for (int anInt : ints) {
               System.out.print(anInt+"\t");
          }
           System.out.println();
      }
  }
}

Comments