0% found this document useful (0 votes)
20 views7 pages

General Orientation and Introduction To Arrays LECTURE

Uploaded by

raquellayson99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

General Orientation and Introduction To Arrays LECTURE

Uploaded by

raquellayson99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Week 1

LESSON 1.0: GENERAL ORIENTATION


AND INTRODUCTION TO ARRAYS
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a
collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

Normally, an array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.

Thing to know about array


– An array is itself an object. You can refer to the array object as a whole rather than a specific element of the array by using the array’s
variable name without an index. Thus, if x[5] refers to an element of an array, x refers to the array itself.
– An array has a fixed length that’s set when the array is created. This length determines the number of elements that can be stored in the
array. The maximum index value you can use with any array is one less than the array’s length. Thus, if you create an array of ten
elements, you can use index values from 0 to 9.
– You can’t change the length of an array after you create the array.
– You can access the length of an array by using the length field of the array variable. For example, x.length returns the length of the array
x.
TYPES OF ARRAY IN JAVA
There are two types of array.
o Single/One Dimensional Array
o Multidimensional Array
SINGLE/ONE DIMENSIONAL ARRAY
An array is a collection of elements of one specific type in a horizontal fashion. The array in contention here is that of the one-dimensional array in
Java programming.

Anything having one-dimension means that there is only one parameter to deal with. In regular terms, it is the length of something. Similarly, as far as
an array is concerned, one dimension means it has only one value per location or index.
MULTIDIMENSIONAL ARRAY
The Java multidimensional arrays are arranged as an array of arrays i.e. each element of a multi-dimensional array is another array. The representation
of the elements is in rows and columns. Thus, you can get a total number of elements in a multidimensional array by multiplying row size with column
size.

So if you have a two-dimensional array of 3×4, then the total number of elements
in this array = 3×4 = 12.

TYPES OF MULTIDIMENSIONAL ARRAY


– Two Dimensional
– Three Dimensional

Two-Dimensional Array
The simplest of the multi-dimensional array is a two-dimensional array. A simple definition of 2D arrays is: A 2D array is an array of one-dimensional
arrays.
In Java, a two-dimensional array is stored in the form of rows and columns and is represented in the form of a matrix.
So if you have an array of 3×3, this means it will have 3 rows and 3 columns.
As shown above, each intersection of row and column stores an element of the 2D array. So if you want to access the first element in the 2d array, then it
is given by [0, 0].

Note that as the array size is 3×3, you can have 9 elements in this array.

Three-Dimensional Array

We already discussed that an array gets more complex as their dimensions increase. Three-dimensional arrays are complex for multi-dimensional arrays. A
three dimensional can be defined as an array of two-dimensional arrays.
– The number of Tables/Arrays: The first dimension indicates how many tables or arrays a 3d array will have.
– The number of Rows: The second dimension signifies the total number of rows an array will have.
– The number of Columns: The third dimension indicates the total columns in the 3d array.

You might also like