answersLogoWhite

0

What is basic of arrays in java?

Updated: 8/9/2023
User Avatar

Wiki User

11y ago

Best Answer

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

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

Wiki User

11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
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
Related questions

How do you find the difference between two arrays in Java?

for arrays you can list the different arrays and what attributes that you give to them.


Where can one find advice about working with Java sorting arrays?

There are many places where one could find advice about working with Java sorting arrays. The best place to learn more about working with Java would be to contact Oracle.


Is it possible to use arrays when using the java programming language?

It is possible to use arrays when employing java programming language. There are many different series of programming choice that can be employed with various end results.


Can ragged arrays created by using java?

I assume you mean that you have a number of rows, and that not all rows have the same number of "cells". Yes, in Java a two-dimensional array is implemented as an array of arrays (each item in the top-level array is, in itself, an array); a 3-dimensional array is an array of arrays of arrays, etc.; and there is no rule stating that all secondary (etc.) arrays must have the same number of elements.


How do you create a program in visual basic?

arrays programms in visual basic


Which one is better than arrays in java?

Better for what? Arrays have their purposes, other constructs have other purposes. Depending on what you need, an array may be just what you need.


How can you prove the given string by user is array or not in java?

Strings and Arrays are two totally different data types in Java and they will not match with one another.


What are associative arrays in Java programming language?

Java does not support associative arrays. However, you can achieve the same thing using a map.


Why c and java arrays are start with zero and pascal arrays are start with one?

It's a difference in mentality; some believe 0 is the begin, and is thus the only logical choice, and some think the opposite.


How the oracle is connected to java and visual basic?

Oracle is the company that owns Java and is partnered with Microsoft who owns Visual Basic.


What are the features of core java?

Core java refers to the core or basic concepts of the Java programming language. Things like encapsulation, inheritance, multi-threading, exception handling and other basic feature of java that comes as part of the Java standard edition forms Core Java


What is the definition of Core Java?

Core java refers to the core or basic concepts of the Java programming language. Things like encapsulation, inheritance, multi-threading, exception handling and other basic feature of java that comes as part of the Java standard edition forms Core Java