answersLogoWhite

0

the different types of arrays are single dimensional, two dimensional and multidimensional arrays..

they can be declared in java as follows

1)for single dimensional:

int x[]=new int[5];

2)for 2-d array:

int x[][]=new int[5][4];

3)for multidimensional array:

combination of both 1 and 2nd declaration is allowed..

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
EzraEzra
Faith is not about having all the answers, but learning to ask the right questions.
Chat with Ezra
More answers

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the mainmethod of the "Hello World!" application. This section discusses arrays in greater detail.

An array of ten elements

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output. class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // etc. anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600; anArray[6] = 700; anArray[7] = 800; anArray[8] = 900; anArray[9] = 1000; System.out.println("Element at index 0: " + anArray[0]); System.out.println("Element at index 1: " + anArray[1]); System.out.println("Element at index 2: " + anArray[2]); System.out.println("Element at index 3: " + anArray[3]); System.out.println("Element at index 4: " + anArray[4]); System.out.println("Element at index 5: " + anArray[5]); System.out.println("Element at index 6: " + anArray[6]); System.out.println("Element at index 7: " + anArray[7]); System.out.println("Element at index 8: " + anArray[8]); System.out.println("Element at index 9: " + anArray[9]); } }

The output from this program is: Element at index 0: 100 Element at index 1: 200 Element at index 2: 300 Element at index 3: 400 Element at index 4: 500 Element at index 5: 600 Element at index 6: 700 Element at index 7: 800 Element at index 8: 900 Element at index 9: 1000

In a real-world programming situation, you'd probably use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as shown above. However, this example clearly illustrates the array syntax. You'll learn about the various looping constructs (for, while, and do-while) in theControl Flow section.

User Avatar

Wiki User

12y ago
User Avatar

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.

Declaring an Array

Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

Declaring an array of primitives:

int[] Values; // brackets before name (recommended)

int Values []; // brackets after name (legal but less readable)

// spaces between the name and [] legal, but bad

Declaring an array of object references:

Ferrari[] Ferraris; // Recommended

Ferrari Ferraris[]; // Legal but less readable

Constructing an Array

Constructing an array means creating the array object on the heap i.e., doing a new on the array type. To create an array object, Java must know how much space to allocate on the heap, so you must specify the size of the array at creation time. The size of the array is the number of elements the array will hold.

Constructing One-Dimensional Arrays

The most straightforward way to construct an array is to use the Keyword new followed by the array type, with a bracket specifying how many elements of that type the array will hold. The following is an example of constructing an array of type int:

int[] myArray; // Declares the array of ints

myArray = new int[4]; // constructs an array and assigns it

// to the myArray variable

The preceding code puts one new object on the heap-an array object holding four elements-with each element containing an int with a default value of 0. Think of this code as saying to the compiler, "Create an array object that will hold four int values, and assign it to the reference variable named myArray. Also, go ahead and set each int element to zero. Thanks."

You can also declare and construct an array in one statement as follows:

int[] myArray = new int[4];

Initializing an Array

Initializing an array means putting things into it. The "things" in the array are the array's elements, and they're either primitive values, or objects referred to by the reference variables in the array. If you have an array of objects (as opposed to primitives), the array doesn't actually hold the objects, just as any other nonprimitive variable never actually holds the object, but instead holds a reference to the object. But we talk about arrays as, for example, "an array of five strings," even though what we really mean is, "an array of five references to String objects." Then the big question becomes whether or not those references are actually referring to real String objects, or are simply null. Remember, a reference that has not had an object assigned to it is a null reference. And if you try to actually use that null reference by, say, applying the dot operator to invoke a method on it, you'll get the most famous NullPointerException.

The individual elements in the array can be accessed with an index number. The index number always begins with zero, so for an array of ten objects the index numbers will run from 0 through 9. Suppose we create an array of three Ferraris as follows:

Ferrari [] raceCars = new Ferrari[3];

We have one array object on the heap, with three null references of type Ferrari, but we don't have any Ferrari objects. The next step is to create some Ferrari objects and assign them to index positions in the array referenced by raceCars:

raceCars[0] = new Ferrari();

raceCars[1] = new Ferrari();

raceCars[2] = new Ferrari();

This code puts three new Ferrari objects on the heap and assigns them to the three index positions (elements) in the raceCars array.

User Avatar

Wiki User

14y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What is basic of arrays in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp