Use a zero-length Array

Source: Internet
Author: User
Assume that you write an application to filter data. For example, to get the data of a given range in an integer array, write a method to remove the data that does not meet the conditions in the array and return a new array.
How can we implement this method? One method is:
Import java. util .*;

Public class zerodemo1 {

// Filter input array and throw away values
// That are less than minval or greater
// Maxval

Static int [] filterdata (INT indata [], int minval, int maxval ){

// Check parameters for errors

If (indata = NULL ){
Throw new nullpointerexception ("indata is null ");
}
If (maxval <minval ){
Throw new illegalargumentexception ("maxval <minval ");
}

// Count Number of valid values
// In input Array

Int validcnt = 0;
For (int ii = 0; II <indata. length; II ++ ){
If (indata [II]> = minval & indata [II] <= maxval ){
Validcnt ++;
}
}

// If no valid values, return null

If (validcnt = 0 ){
Return NULL;
}

// Copy valid values to new array
// And return it

Int outdata [] = new int [validcnt];
For (int ii = 0, j = 0; II <indata. length; II ++ ){
If (indata [II]> = minval & indata [II] <= maxval ){
Outdata [J ++] = indata [II];
}
}
Return outdata;
}

Public static void main (string ARGs []) {

// Set up test array of Integers

Int indata [] = new int [] {1, 3,-17, 8, 59 };

// Filter out values not in the range 1-10

Int outdata1 [] = filterdata (indata, 1, 10 );
For (int ii = 0; II <outdata1.length; II ++ ){
System. Out. println (outdata1 [II]);
}

// Filter out values not
// In the range 100-200

Int outdata2 [] = filterdata (
Indata, 100,200 );
For (int ii = 0; II <outdata2.length; II ++ ){
System. Out. println (outdata2 [II]);
}
}
}

The filterdata method traverses the input array twice and calculates the number of valid data for the first time. Initialize a new array based on the data and copy the valid data. If no valid data exists, the method returns NULL.
The execution result of zerodemo1 is:
1
3
8
Exception in thread "Main"
Java. Lang. nullpointerexception
At zerodemo1.main (zerodemo1.java: 72)

This is a very simple application. When filterdata is called for the second time, null is returned, and an exception is thrown.

If there is no valid data, there is a better implementation method:
/*
If (validcnt = 0 ){
Return NULL;
}
*/

If no valid data exists, we can allocate a zero-length array:

Int outdata [] = new int [0];

This is an absolutely legal Java usage.

In the zerodemo example, if validcnt is often 0, that is, the given data is often invalid data, you can change the program chip:

Int outdata [] = new int [validcnt];
If (validcnt = 0 ){
Return outdata;
}

This usage is equivalent:

Int outdata [] = new int [] {};

This method is also valid. It initializes a zero-length array.

Generally, returning a null array is not a good choice. When an array is returned, your algorithm is greatly improved even if the length of the array is zero. If the program often returns a zero-length array, you can declare a static constant as follows to improve efficiency:

Private Static final int [] zero_length_array = new int [0];

This constant can be shared in the used applications.

The following example illustrates another application of the zero-length array:

Import java. util .*;

Public class zerodemo2 {
Public static void main (string ARGs []) {

// Set up arraylist and add strings to it

List stringlist = new arraylist ();
Stringlist. Add ("string 1 ");
Stringlist. Add ("string 2 ");
Stringlist. Add ("string 3 ");

// Convert to String Array

String out [] = (string []) stringlist. toarray (New String [0]);
For (int ii = 0; II <out. length; II ++ ){
System. Out. println (out [II]);
}
}
}

Running result of zerodemo2 program:
String 1
String 2
String 3

The program creates an arraylist object and adds three strings. Then the program calls the toarray method. In the example, the parameter of the toarray method is "New String [0]".

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.