Computer Science, asked by sarahkathuria15, 5 months ago

What exactly is an array in java and how do you construct one?

Answers

Answered by bindureddy30
1

An array is a group (or collection) of same data types. For example an int array holds the elements of int types while a float array holds the elements of float types.

The first way is to use the new operator to create a new instance of an array: String[] names = new String[10]; That line creates a new array of Strings with 10 slots (sometimes called elements). When you create a new array object using new, you must indicate how many slots that array will hold.

hope this helps you

Mark as brainliest

Answered by Manas934
0

An array is a container object that can store multiple values of homogeneous ie. same data type.

How to construct an array:

There are two ways to construct an array.

1. The first way of constructing an array involves direct input of values.

Syntax-

datatype arrayname[] = {element1, element2,..... element n};

In this method an array is created with a primitive data type followed by open and close square braces. After the equal to sign a curly bracket is opened in which you have to input the elements of the array of the mentioned data type seperated by commas. Then the curly braces are closed. Thus you have an array.

2. In this method you can take the input from the user.

Syntax-

datatype arrayname[] = new datatype[n];

for(int i=0; i<n; I++)

{

arrayname[I] = scan. nextdatatype() ;

}

In this method you have to use the scanner class or buffered reader to take input from then user. Create an object of the input class. Then create an array and allocate the memory of the array using the new keyword. n represents the number of elements of the array. Then use the for loop to take input at each index number of the array.

Hope you like the explanation.

Similar questions