Create an integer array A initialised with any numbers create another array V which has the square of the elements in A Display both the arrays
Answers
Creating and Using Arrays
Here's a simple program, called ArrayDemo (in a .java source file), that creates the array, puts some values in it, and displays the values.
public class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declare an array of integers
anArray = new int[10]; // create an array of integers
// assign a value to each array element and print
for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[i] + " ");
}
System.out.println();
}
}
The output from this program is:
0 1 2 3 4 5 6 7 8 9
This section covers these topics:
Declaring a Variable to Refer to an Array
Creating an Array
Array Initializers
Accessing an Array Element
Getting the Size of an Array
Declaring a Variable to Refer to an Array
This line of code from the sample program declares an array variable:
int[] anArray; // declare an array of integers
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written type[], where type is the data type of the elements contained within the array, and [] indicates that this is an array. Remember that all of the elements within an array are of the same type. The sample program uses int[], so the array called anArray will be used to hold integer data. Here are declarations for arrays that hold other types of data: