Arrays_in_Java
Arrays_in_Java
It’s like a container to keep It’s a Data Structure Helps to organize same type
similar data in a single- of variables
ordered list
Let’s consider an example- A deck of cards
If you take a card, you must decide how to represent a card in a program
You could create 52 string variables, named card1, card2, etc then load
each variable with a description such as card1 = “Ace of Clubs”; card2 =
“Two of Diamonds”; etc.
However the problem with this approach is that you will have to deal
with 52 different variables, and there is no good way to manipulate all
the variables as a whole for operations such as shuffling and dealing
A better approach is to group all the data together in a structured way, in
an array called deck that has 52 entries ranging from 1 to 52.
Now you only have one
variable to worry about
(with 52 entries)
You can easily access
and manipulate any
element of the array by
referring to its element
number (or subscript)
but can still manipulate
the deck as a whole as
well.
What is An Array in Computer
Science?
In Computer Science, an array is
defined as a group of memory locations
(variables) stored continuously in
memory array
Each has the same name and are all of
the same type
Each variable in the array is called a
member, or element and is identified by
a number called an index
For example, an array of integers called
array can be visualized as:
What is An Array in Computer
Science? (contd.)
In most programming languages, indexing begins with 0
The numbers inside the parenthesis after the array name indicate
which element you are referring to
The number inside parenthesis is called the arrays subscript or the
index.
Arrays are objects that take up memory
(just like regular variables do)
However, unlike regular variables, arrays
as objects need to be allocated memory
Declaring or dynamically using the operator new
0 1 2 3 4
? ? ? 301 ?
All arrays in Java begin their indexing at 0
Array subscripts are of an unknown value when initialized
Another option is to just declare an array with the name and type,
and later allocate the memory to it
However, if you try to assign to a subscript before you allocate
memory to the array, an error occurs, for example:
This declares an array of type int called coins with size 7 (indexed 0-
6) and all elements are set to the pre-assigned values (this will
automatically allocate the necessary memory for the array)
Multiple array
declarations are also
possible, for example.
In addition to creating multiple arrays in one declaration statement,
you can also create and initialize multiple arrays in one declaration
statement, as seen here.
You can use the individual elements
of an array, by accessing them
using the arrays subscript value in
your code
Arrays
3 (0,1,2,3 – has 4 elements)
This is important when looping through an array, for
example:
A range error is a common error that can occur
using arrays
Arrays Declaration
Today
we
Arrays Initialization
learned:
Using Arrays
Any Questions?