, PART A
1.1 Introduction to array
An array is an ordered sequence of the same type of data. In Java, an array is an ordered
indexed collection of data values of the same type. Most of the algorithm and problem-
solving approaches use array in their implementation. An array consists of its data type, size,
index and value. The elements are stored at contiguous memory locations in an array. Array
have a fixed size where the size of the array is defined when the array is initialized.
1.2 Declare, create, and initialize an array
To declare, create, and initialize an array in a programming language, first, you declare the
array by specifying its type and name. For example, in Java, you would declare an array of
integers like this: "int [] myArray;". Then, you create the array by allocating memory for it.
This is done using the "new" keyword in Java: "myArray = new int [5];", which creates an
array of integers with a size of 5. Finally, you initialize the array by assigning values to its
elements. This can be done individually using index notation: "myArray [0] = 1;", "myArray
[1] = 2;", and so on, or you can initialize the array during declaration: "int [] myArray = {1, 2,
3, 4, 5};". This process allows for the efficient storage and manipulation of data in the array
for various programming tasks.
Declare an array
Syntax : DataType[] ArrayName;
Examples to declare an array :
int[] list;
String[] fruits;
float[] prices;
Creating an array
There are two ways to do this which are using new operator and directly initialising the
contents of the array.
a) Using new operator
Arrayname is the name of the array. Datatype is the type of elements the array is going to
store. SIZE is the number of elements that will be stored in the array
Syntax : Arrayname = new Datatype[SIZE];
Examples :
list = new int[10];
fruits = new String[5];
3
prices = new float[4];
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller nurafiezah. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $13.29. You're not tied to anything after your purchase.