Java Custom Length Variable Array Operation

  • 2021-08-12 02:52:58
  • OfStack

We all know that arrays are linear, fixed in type, continuous in memory address and fixed in length. Mainly, if the array 1 is defined, its length will be fixed, and only limited data can be added. The variable length array is to break this length and realize the infinite increase of array data

Then defining an array with variable length can use two arrays to realize the change of array length. In order to avoid re-opening space every time data is added or deleted, I first set the original array as a fixed length, and when the array is full, I increase the length by 1 fixed at a time, which saves the time of opening up space

Because the data type in the array is indeterminate, it is better to use generics


public class MyList<E> {
 private int rongliang;// Capacity 
 private int zengliang;// Increment 
 private int num;// Quantity 
 
 // Definition 1 Original array 
 //Object Class contains all types, so the array is defined using the Object Class 
 private Object[] src;
 
 //3 Different construction methods 
 public MyList(){
 this(10,10);
 }
 
 public MyList(int rongliang){
 this(rongliang,10);
 }
 
 public MyList(int rongliang,int zengliang){
 this.rongliang = rongliang;
 this.zengliang = zengliang;
 src = new Object[rongliang];
 }
}

In MyList, add data to the array. When the number of data in the array is less than the length of the array, you can add data directly to the null, but when the number of arrays is greater than or equal to the length of the array, you should first redefine an array, the length is the original array plus increment, and then add data


public void add(E s){
 // Determine the amount of data in the array num Is it larger than the length (capacity) of the array, and if it exceeds it, it needs to be expanded 
 if(num>=src.length){
 // Definition 1 New array, the length is the original length plus increment 
 Object arr[] = new Object[src.length+zengliang];
 // Copy array data 
 System.arraycopy(arr, 0, arr, 0, src.length);
 src = arr;
 }
 // If num If it is not larger than the length of the array, it does not need to be expanded and is directly added 
 // If num Is greater than or equal to the array length, the above if Statement expansion, and then add data 
 // Finally num++
 src[num++] = s;
 }

Take out the specified subscript data, because the subscript parameter is passed in, so it is necessary to judge whether the subscript of the array is out of bounds and throw an exception


public E get(int index){
 // Throw an exception 
 if(index<0 || index>=num){
 throw new IndexOutOfBoundsException(" Subscript out of bounds! index:"+index+",size:"+num);
 }
 // Cast to E Type 
 return (E)src[index];
 }

Modify the specified subscript data, because the subscript parameter is passed in, so it is necessary to judge whether the subscript of the array is out of bounds and throw an exception


public void modify(int index,E s){
 // Throw an exception 
 if(index<0 || index>=num){
 throw new IndexOutOfBoundsException(" Subscript out of bounds! index:"+index+",size:"+num);
 }
 src[index] = s;
 }

Delete the specified subscript data. When the length of null value in the array is greater than or equal to the increment, reduce the capacity of the array to prevent waste


public void delete(int index){
 // Throw an exception 
 if(index<0 || index>=num){
 throw new IndexOutOfBoundsException(" Subscript out of bounds! index:"+index+",size:"+num);
 }
 // Will >index The data of moves forward in turn 1 Bit 
 System.arraycopy(src, index+1, src, index, num-index-1);
 num--;
 // Methods to reduce capacity 
 if(src.length-num>=zengliang){
   // Definition 1 A new array whose length is the length of the original array minus the increment 
     Object arr[] = new Object[src.length-zengliang];
   // Copy array 
     System.arraycopy(src, 0, arr, 0, num);
   src = arr;
 }
 }

Change the data at the specified subscript to the specified data


public void insert(int index,E s){
 // Throw an exception 
 if(index<0 || index>=num){
 throw new IndexOutOfBoundsException(" Subscript out of bounds! index:"+index+",size:"+num);
 }
 // Determine the amount of data in the array num Is it larger than the length (capacity) of the array, and if it exceeds it, it needs to be expanded 
 if(num>=src.length){
   // Definition 1 New array, the length is the original length plus increment 
   Object arr[] = new Object[src.length+zengliang];
   // Copy array data 
   System.arraycopy(src, 0, arr, 0, src.length);
   src = arr;
 }
 // Will >index The data of moves backward in turn 1 Position 
 //arraycopy() You can copy the data to yourself 
 System.arraycopy(src, index, src, index+1, num-index);
 // Insert data 
 src[index] = s;
 num++;
 }

Finally, write an array to get the number of data in the array, not the length of the array


public int size(){
   return num;
 }

Write a test class to test whether this variable-length array is feasible


public class test {
 
 public static void main(String[] args) {
 // Create 1 A MyList Object 
 //  Clarify the type when creating an object 
 MyList<String> list = new MyList<String>();
 
 // Add data 
 list.add("a");
 list.add("b");
 list.add("c");
 list.add("d");
 list.add("e");
 list.add("f");
 list.add("g");
 list.add("h");
 list.add("i");
 list.add("j"); 
 
 // Traversing an array 
 for(int i=0;i<list.size();i++){
 String s = list.get(i);
 System.out.print(s+" ");
 }
 
 System.out.println("");
 int n = list.size();
   System.out.println(" The number of data is: "+n);
 System.out.println("**********************************************");
 
 // Modify the data at the specified location 
 list.modify(1, "QQ");
 
 // Traversing an array 
 for(int i=0;i<list.size();i++){
 String s = list.get(i);
 System.out.print(s+" ");
 }
 
 System.out.println("");
 int m = list.size();
 System.out.println(" The number of data is: "+m);
 System.out.println("**********************************************");
 
 // Delete data at a specified location 
 list.delete(2);
 
 // Traversing an array 
 for(int i=0;i<list.size();i++){
 String s = list.get(i);
 System.out.print(s+" ");
 }
 
 System.out.println("");
   int k = list.size();
   System.out.println(" The number of data is: "+k);
 System.out.println("**********************************************");
 
 // Inserts the specified data at the specified location 
 list.insert(3, "zr");
 list.insert(3, "qi");
 
 // Traversing an array 
 for(int i=0;i<list.size();i++){
 String s = list.get(i);
 System.out.print(s+" ");
 }
 
 System.out.println("");
 int h = list.size();
   System.out.println(" The number of data is: "+h);
 System.out.println("**********************************************");
 }
 
}

The result of the final array is:


a b c d e f g h i j 
 The number of data is: 10
**********************************************
a QQ c d e f g h i j 
 The number of data is: 10
**********************************************
a QQ d e f g h i j 
 The number of data is: 9
**********************************************
a QQ d qi zr e f g h i j 
 The number of data is: 11
**********************************************

Add: Create an array of 1 custom length in Java and enter each element

Knowledge points used: array, method, Scanner, for loop.

Homework:


package Array;
import java.util.Scanner;
public class InputArray {
public static void main(String[] args) {
shuzu();// Method invocation  
 }
 // Method definition 
 public static void shuzu() {
 
 // Take the number entered as the length of the array 
 Scanner sz = new Scanner(System.in);
 System.out.println(" Please enter the length of the array :");// Prompt can be operated 
 int[] cd = new int[sz.nextInt()];// Array initialization complete 
 System.out.println(" The current array length is defined as: "+cd.length);// Prompt again 1 Next result 
 
 // Use for Loop for each 1 Element assignment 
 for (int i = 0; i < cd.length; i++) {
  int q = i+1;// Here q Used as a prompt to avoid prompting the 0 Elements. 
  System.out.println(" Please enter the "+q+" The value of the elements: ");
  cd [i] = sz.nextInt();
  System.out.println(" No. 1 "+q+" Elements are defined as "+cd[i]+" . "); 
  }
  sz.close();
  
  // The elements in the array have been assigned, but the for Cyclic traversal 1 Times 
 System.out.print(" All elements in the array are assigned: ");// Continue prompt 1 Under 
 for (int i2 = 0; i2 < cd.length; i2++) {  
  if(i2 == cd.length-1) {
  System.out.print(cd[i2]+" . ");
  }else {
  System.out.print(cd[i2]+" , ");
  }
 }
 return;// Method ends, rentun ; 
 }
 
}

Related articles: