answersLogoWhite

0


Best Answer

For example we have a class :

public class Test()

{

public void print()

{

System.out.println("Hello");

}

}

So, if you want to create array from Test type. You can try something like this :

Test[]myTestArray=new Test[2];

myTestArray[0]=new Test();

myTestArray[1]=new Test();

I hope it will help.

User Avatar

Wiki User

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

Wiki User

14y ago

Step 1) Declaration

Step 2) Instantiation

Declaration in Java

int[] array;

Instantiation in Java

array = new int[size];

Declaration and Instantiation in Java

int[] array = new int[size];

Declaration in C

int* array;

Instantiation in C

array = malloc(sizeof(int) * size);

Declaration and Instantiation in C

int array[size];

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

When you have more than one data-element.

Example: second parameter of function main, called argv, is an array of strings.

When you want to refer to a series of memory locations with same name and access them using an index, we use array

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It really depends on which program you write in, if you are writing a program that displays days, and months, you need string arrays.

However, if you write programs that deal with numbers(test marks, prices of products, etc), you will need an int array.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

You use an array when all you really need is a sequence of data of the same type. Arrays also offer constant-time random access, making them the ideal container for sorting algorithms. The downside to arrays is that inserting new elements may require the entire array to be re-allocated to new memory. This can be alleviated by allocating more memory than you need. Moreover, inserting anywhere but at the end of the array also requires all elements that follow the insertion point to be right-shifted by one element to make room for the new element. Similarly, when extracting elements, all elements that follow must be left-shifted to remove the gap.

As a rule of thumb, you should consider an array to be the default sequence container unless you have good reason to use another type of container. For instance, if you plan on performing a lot of insertions into the middle of a sequence or at the beginning of a sequence, a linked list might be a better option. Access will be slower due to the need to dereference node pointers and you will lose constant-time random access to all but the head (or tail) of the list. Similarly, if you wish to maintain a sequence in sorted order, a binary tree or ordered map (red/black tree) might be better options.

Note that there is no single container type that is ideally suited to every task and every algorithm you might perform upon a container. Your choice of container is often a compromise between performance and memory consumption but, where there is a choice available, testing is the only way to determine which, if any, is the most appropriate container.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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

When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, Values is a reference to an int array object, and not an int primitive.

We can also declare multidimensional arrays, which are in fact arrays of arrays. This can be done in the following manner:

String[][][] occupantName; // recommended

String[] ManagerName []; // Real bad coding practice, but legal

The first example is a three-dimensional array (an array of arrays of arrays) and the second is a two-dimensional array. Notice in the second example we have one square bracket before the variable name and one after. This is perfectly legal to the compiler, but if you write code like this, anyone who is going to manage your code in future is sure going to curse you badly.

It is never legal to include the size of the array in your declaration. Yes, before you ask me, let me admit that you can do that in some other languages, which is exactly why you might see a question or two that include code similar to the following:

int[5] races;

The preceding code won't make it past the Java compiler. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters and not when you declare the array variable.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

You use an array to efficiently store data sequences with constant-time random-access.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What two steps are needed to create an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Could array may have elements that are numbers and strings?

In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.


How do you merge two array without using function?

Take another array big enough to hold both array copy content of these two array into new one. You merged two array and haven't used a single function.!


Explain the Different types of array?

one dementional array and two dementional array


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


How do you swap two adjecent no in array in c?

Option 1) Use a temporary variable: int x = array[i]; array[i] = array[i+1]; array[i+1] = x; Option 2) Use bit operators: array[i] ^= array[i+1] ^= array[i];

Related questions

How you create table in c language?

The simplest way to create a table in C is to use a two-dimensional array.


How can two single dimensional array values be stored in a third single dimensional array?

You need to create a new array with enough elements to cater for both arrays. Thus if the first array has 10 elements and the second has 5, you must create a 15 element array to store both. You then copy elements from the first array into the third and immediately follow with the elements from the second. Note that the first two arrays must be of the same type. You cannot combine an array of numeric values with an array of strings, for instance.


Could array may have elements that are numbers and strings?

In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.


What two things are needed to create fraternal twins?

Two eggs and Two Sperm.


What is a table array?

A two-dimensional array.


Create an anonymous array in java?

An anonymous array in Java is just an array with its contents declared at instantiation. Normal array declaration: int[] nums = new int[3]; nums[0] = 0; nums[1] = 1; nums[2] = 2; Anonymous array declaration: int[] nums = new int[] {0,1,2}; The two examples above will each result in the same array.


How do you merge two array without using function?

Take another array big enough to hold both array copy content of these two array into new one. You merged two array and haven't used a single function.!


Explain the Different types of array?

one dementional array and two dementional array


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


How does two dimensional array differ from single dimensional array?

A one dimensional array is a scalar value repeated one or more times.A two dimensional array is an array of one dimensional arrays.A three dimensional array is an array of two dimensional arrays, and so forth.The one dimensional array is like a list of things, where the two dimensional array is like an array of things. (Think one row of a spreadsheet versus the whole spreadsheet.)[addendum]Every level of array depth is also a level of pointer depth. For example: A 3 dimensional int array is an int***. So a one dimensional int array is an int*, and a two dimensional int array is an int**. This is only important if you are doing pointer work, but it can become very important.


What is a multi array?

A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right. A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables. Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.


What is the array of string in c?

A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.