LESSON 3 Flashcards
(31 cards)
Java provides a data structure, the__, which stores a fixed-size sequential collection of elements of the same type. An __ is used to store a collection of data, but it is often more useful to think of an __as a collection of variables of the same type.
Array
To declare an array, define the variable type with __
square brackets
Declaring an array does __it. In order to store values in the array, we must initialize it first
not initialize
Different ways to initialize an array:
- Initializing an array without assigning values
- Initializing an array after a declaration
- Initializing an array and assigning values
An array can be initialized to a particular size. In this case, the default value of each element is 0.
- Initializing an array without assigning values
An array can also be initialized after declaration.
Note: When assigning an array to a declared variable, the new keyword must be used.
- Initializing an array after a declaration
An array can also be initialized during declaration.
Note: __an array during initialization, the size is not specified.
- Initializing an array and assigning values
Each variable in Java array is also called an __
“element”
Each element in the array has an __ .You can access each element in the array via its __.
Index (a number)
You can use the ___ in a Java array just like if they were ordinary variables.
Elements
You can read their value, assign values to them, use the elements in ___ as parameters to method calls.
calculations and pass specific elements
The indexes of elements in a Java array always start with _ and continue to the number 1 below the size of the array.
0
Java provides an attribute 1.__ that
determines the 2.__.
- length
- length of an array
Every array has an __ property whose value is the size of the array.
in-built length
The length property can be invoked by using the __ followed by the array name. We can find the length of int[], double[], String[], etc.
dot (.) operator
__ is an array of type int that can hold 5 elements.
arr
The __ is a variable that stores the length of an array.
arrayLength
Note that __ determines the maximum number of elements that the array can contain or the capacity of the array. It does not count the elements that are inserted into the array. That is, __ returns the total size of the array. For arrays whose elements are initialized at the time of its creation, length and size are the same.
length
SORTING ELEMENTS IN AN ARRAY
•Using For Loops
•Using The Sort method
You can use__ to traverse the array and compare adjacent elements while traversing and putting them in order.
Using For Loops
The Arrays class of ‘java.util’ package provides the __that takes an array as an argument and sorts the array. This is a direct __ and you can sort an array with just one method call.
Using The Sort method
The __provided by ‘java.util.Arrays’ class is a very simple and faster way to sort an array. This method can sort elements of primitive types as well as objects that implement the comparable interface.
SORT METHOD
The general prototype of sort method is as follows:
A. Sort Numeric Array In Ascending Order
B. Sort Numeric Array In Descending Order
C. Sort String Array in Alphabetical Order
The first demonstration is sorting of number array in __ using sort methods. As already mentioned, by default the sort method sorts the array in__. Thus, to sort a numeric array in __, you just have to call the method on the array in question.
A. Sort Numeric Array In Ascending Order