Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Arrays_in_Java

Uploaded by

Sahaj Khurana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Arrays_in_Java

Uploaded by

Sahaj Khurana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Let’s Plan a Party !!

Which lists will you make for


the party?
What Are Arrays?
 Up to this point in your Computer Science education, you have been
manipulating data by using simple variables to store the data
 Variable data types are known as simple or primitive data types
 Now we will examine the first complex or advanced data type, known as an
array
 An array is a container object that holds a fixed number of values of a single
type
 Often we work with data that is part of a group with some overall structure. For
instance, we could be working with a page of text rather than the individual
characters, or working with a book, rather than the individual pages
What Are Arrays?

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

Allocating  When we declare the array, we specify


the array type, array name, and array size
Arrays (which is optional at the time of
declaration)
 Optionally as well, we can also initialize
all array elements at the time it is
declared
For Example:

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:

 It Generates this error:


Initializing Arrays
 To avoid the errors seen previously, we can also declare and initialize
arrays in the same statement using curly braces { } and commas
 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

Using However, to access the entire


array, or to make assignments to
the entire array, most often in
Arrays Computer Science, a loop is used

Specifically, a for loop


 A ‘built-in method’ available for all arrays,
the .length feature returns the size (number of
elements) of an array
 Keep in mind that the size of an array (number of
elements) is not the same as the subscript of the
last element (because arrays start counting at 0)
Using  So an array with a size of 4, has the last subscript at

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

Range Errors  This is a run-time error that occurs when your


code refers to an array element that is outside
the defined subscript values, for example:
What are Arrays?

Arrays Declaration
Today
we
Arrays Initialization
learned:
Using Arrays
Any Questions?

You might also like