Review the Array

Source: Internet
Author: User
Review the array-general Linux technology-Linux programming and kernel information. The following is a detailed description. Arrays have been introduced in this book before. Now that you have understood the class, you can introduce an important point about Arrays: arrays are implemented as objects. Therefore, you may want to take advantage of a special attribute of an array, specifically, the size of an array-that is, the number of elements that an array can store-which can be found in its length instance variable. All arrays have this variable, and it always saves the size of the array. The following program demonstrates this nature:

// This program demonstrates the length array member.
Class Length {

Public static void main (String args []) {int a1 [] = new int [10]; int a2 [] = {3, 5, 7, 1, 8, 99, 44,-10 }; int a3 [] = {4, 3, 2, 1 };

System. out. println ("length of a1 is" + a1.length );
System. out. println ("length of a2 is" + a2.length );
System. out. println ("length of a3 is" + a3.length );

}
}

The program displays the following output:

Length of a1 is 10
Length of a2 is 8
Length of a3 is 4

We can see that the size of each array is displayed. Remember that the length value is irrelevant to the number of actually used elements in the array. Length only reflects the number of elements that an array can contain.

In many cases, you can make good use of length. For example, the following Program is an improved version of the Stack class. You may recall that early versions of this class always generate a stack of 10 elements. The following version allows you to generate a stack of any length. The stck. length value is used to prevent stack overflow.

// Improved Stack class that uses the length array member.

Class Stack {private int stck []; private int tos;

// Allocate and initialize stack

Stack (int size ){
Stck = new int [size];
Tos =-1;

}

// Push an item onto the stack void push (int item) {if (tos = stck. length-1) // use length member System. out. println ("Stack is full. ");
Else
Stck [++ tos] = item;
}

// Pop an item from the stack int pop (){

If (tos <0) {System. out. println ("Stack underflow."); return 0;

}
Else
Return stck [tos --];
}
}

Class TestStack2 {

Public static void main (String args []) {
Stack mystack1 = new Stack (5 );
Stack mystack2 = new Stack (8 );
// Push some numbers onto the stack
For (int I = 0; I <5; I ++) mystack1.push (I );
For (int I = 0; I <8; I ++) mystack2.push (I );

// Pop those numbers off the stack
System. out. println ("Stack in mystack1 :");
For (int I = 0; I <5; I ++)

System. out. println (mystack1.pop ());

System. out. println ("Stack in mystack2 :");
For (int I = 0; I <8; I ++)
System. out. println (mystack2.pop ());
}
}

Note that this program creates two stacks: one with five elements and the other with eight elements. It can be seen that the fact that arrays keep their own length information makes it easy to create stacks of any size.

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.