answersLogoWhite

0


Best Answer

Yes. We can create an array of zero length in JAVA. Here is an example:

class ForFun

{

int[] nullArray=new int[0];

public someMethod()

{

for(int j=0; j

System.out.print(nullArray[j]);

}

}

In the above program, an array named nullArray is created and is allocated memory. But its size is zero and we cannot insert any value into it. It just occupies the memory. Hope this answers your question. :)

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

You can define zero-length arrays in Java in a few different ways:

Object[] objs = new Object[0];

Object[] objs = {};

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Zero-length arrays are rarely "required," but are more often used simply to show that there is no data being passed to a method.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you required zero length array in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why does indexing start with 0 in Java?

Primarily to be compatible with C and C++, which was one of the goals of Java when it was being designed (minimize the learning curve for those familiar with C and C++ to increase adoption). Speaking from a lower-level perspective, arrays are accessed by a pointer and an index. If you call the pointer PTR, and the index IDX, you can access an element in the array by using PTR+IDX. In order to avoid wasting memory, IDX may be zero, since PTR is already allocating that memory to the existence of the array. In languages where IDX starts at 1, PTR[0] stores the number of elements in the array, and can't be directly accessed. Java stores the length of the array elsewhere (in the variable "length"), and so it can start its element allocation at zero.


Why numeric arrays do not end with a '0' in C programming language?

The value zero is a perfectly valid numeric array element in its own right. If we used the value zero to mark the end of a numeric array in the same way we use a null-terminator to mark the end of a string (a character array), we'd have no way of representing the value zero within the array itself. Thus when working with numeric arrays, we must keep track of the array length independently of the array.


How do you find greatest no using array in java?

Assume that the greatest number is the first element (subscript zero). Compare with each element in the array (starting with subscript one), and every time you find one that is greater than the greatest so far, set your variable "greatest" to this number.


How do you declare an array on Pascal?

type array-identifier = array[index-type] of element-type; array-identifier : the name of your array index-type : any scaler except real element-type : the type of element The index type defines the range of indices and thus the number of elements to allocate. For example, [0..41] will allocate 42 elements indexed from 0 to 41, thus creating a zero-based array. If you require a one-based array, use [1..42] instead. Regardless of the range of indices, the first element is always at the lowest address of the array (the compiler will convert your index range into a zero-based range automatically). The element-type determines the length of each element in the array. Multiplying the element length by the number of elements gives the total amount of memory allocated to the array.


What is the container object that holds a fixed number of values of single type?

There are multiple answers to this question, but the most basic one common to both Java and C is the array. An array is a simple structure that you initialize to a certain size and fill with data. Think of an array as a sort of list, where each element in the list is numbered starting from zero up to the list size minus one (or the array is zero-based, as it's also called). In Java: // 10 is the number of elements the array can hold int[] myIntArray = new int[10]; myIntArray[0] = 2; // The first element in the array myIntArray[9] = 4; // The last element in the array Referencing myIntArray[10] or higher will cause a runtime error in Java, which may stop your program. In C: // 10 is the number of elements the array can hold int myIntArray[10]; myIntArray[0] = 2; // The first element in the array myIntArray[9] = 4; // The last element in the array Referencing myIntArray[10] or higher results in a buffer overflow in C (and C++). Note that in C, this won't throw errors like they do in Java, and this can and very likely will cause your program to have random bugs and possibly even crash from a segmentation fault, so be a bit more careful about using arrays in C.

Related questions

Why does indexing start with 0 in Java?

Primarily to be compatible with C and C++, which was one of the goals of Java when it was being designed (minimize the learning curve for those familiar with C and C++ to increase adoption). Speaking from a lower-level perspective, arrays are accessed by a pointer and an index. If you call the pointer PTR, and the index IDX, you can access an element in the array by using PTR+IDX. In order to avoid wasting memory, IDX may be zero, since PTR is already allocating that memory to the existence of the array. In languages where IDX starts at 1, PTR[0] stores the number of elements in the array, and can't be directly accessed. Java stores the length of the array elsewhere (in the variable "length"), and so it can start its element allocation at zero.


Why numeric arrays do not end with a '0' in C programming language?

The value zero is a perfectly valid numeric array element in its own right. If we used the value zero to mark the end of a numeric array in the same way we use a null-terminator to mark the end of a string (a character array), we'd have no way of representing the value zero within the array itself. Thus when working with numeric arrays, we must keep track of the array length independently of the array.


What is array indexing?

Unlike ordinary variables, the variables within an array do not have any names; they are anonymous. To access them you need to use memory offsets from the start of the array. Since the elements of an array are all the same type they are also the same length, thus the offsets are equal to the length of the array type. However, there is no need to calculate the offsets because each element's offset has a zero-based index. Thus the second element can be found at offset index 1.


How do you find greatest no using array in java?

Assume that the greatest number is the first element (subscript zero). Compare with each element in the array (starting with subscript one), and every time you find one that is greater than the greatest so far, set your variable "greatest" to this number.


How do you declare an array on Pascal?

type array-identifier = array[index-type] of element-type; array-identifier : the name of your array index-type : any scaler except real element-type : the type of element The index type defines the range of indices and thus the number of elements to allocate. For example, [0..41] will allocate 42 elements indexed from 0 to 41, thus creating a zero-based array. If you require a one-based array, use [1..42] instead. Regardless of the range of indices, the first element is always at the lowest address of the array (the compiler will convert your index range into a zero-based range automatically). The element-type determines the length of each element in the array. Multiplying the element length by the number of elements gives the total amount of memory allocated to the array.


What is the container object that holds a fixed number of values of single type?

There are multiple answers to this question, but the most basic one common to both Java and C is the array. An array is a simple structure that you initialize to a certain size and fill with data. Think of an array as a sort of list, where each element in the list is numbered starting from zero up to the list size minus one (or the array is zero-based, as it's also called). In Java: // 10 is the number of elements the array can hold int[] myIntArray = new int[10]; myIntArray[0] = 2; // The first element in the array myIntArray[9] = 4; // The last element in the array Referencing myIntArray[10] or higher will cause a runtime error in Java, which may stop your program. In C: // 10 is the number of elements the array can hold int myIntArray[10]; myIntArray[0] = 2; // The first element in the array myIntArray[9] = 4; // The last element in the array Referencing myIntArray[10] or higher results in a buffer overflow in C (and C++). Note that in C, this won't throw errors like they do in Java, and this can and very likely will cause your program to have random bugs and possibly even crash from a segmentation fault, so be a bit more careful about using arrays in C.


How do you start array list from 1?

Array indices are zero-based because the first element is at offset zero from the start of the array. There is no such thing as a one-based array in C. Some other languages do allow you to specify the base of an array, but converting a non-zero-based index to a zero-based index adds a runtime overhead to the implementation.


How do you read data from array?

Use the array suffix operator [] to access the individual elements of an array through a zero-based index.


What do array subscripts always have?

Array subscripts always have a zero-based index. In languages that allow an n-based index, the index is simply offset by n elements, so the compiler subtracts n from the given index to obtain the zero-based index. Arrays are always zero-based because the first element of an array is found zero elements from the start of the array.


Why the array is starting from zero?

By design; it makes the compiler's work easier. 1-based array's addressing-function: Address (array, index) = Address (array) + (index-1)*Elemsize(array) 0-based array's addressing-function: Address (array, index) = Address (array) + index*Elemsize (array)


If array is full how you apply linear search?

An array in C is structured so that it has no particular size; you have to know ahead of time what the dimensions are.So, a linear search means that you go from the first element to the last, either finding the element in the table, or going to the very last element and not finding it.Arrays in C can be zero-terminated, in which case you get the element that does not have a value, and that indicates the value you are searching for is not there.If the array is not zero terminated then you can calculate the dimension of the array, or apply the sizeof operator times the size of the first element to determine the length of the search.


What are sparse matrixes?

A sparse matrix is an array with more zero values than non-zero values.