Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

ArrayList vs LinkedList

1) ArrayList internally uses dynamic array to store the elements. >> LinkedList internally uses doubly linked list to store the elements. 2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory. >> Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory. 3) ArrayList class can act as a list only because it implements List only. >> LinkedList class can act as a list and queue both because it implements List and Deque interfaces. 4) ArrayList is better for storing and accessing data. >> LinkedList is better for manipulating data.

What Is a Package?

>> A package is a namespace that organizes a set of related classes and interfaces. >> Conceptually you can think of packages as being similar to different folders on your computer. >> Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

'length' constant

>> A public constant, called' length', is held in the array object and stores the size of the array. >> It can be used to check for the bounds of an array. >> The length constant, which is an integral part of each array, can be used when the array size is needed without having to create a separate constant. >> Using the above example: prices.length has the value 25.

Definition of an Array

>> An array is a list of values. >> Each value is stored at a specific, numbered position in the array.

Array

>> An array is a simple but powerful programming language construct used to group and organize data. >> Array declares one variable that can hold multiple, individually accessible values. >> Avoids the problem of having to declare separate variables for each piece of data in a large data set.

Array size

>> An array of size N is indexed from 0 to N-1. >> There are N elements in the array.

Passing an element of an Array

>> An element of an array can be passed to a method as well. >> If the element type is a primitive type, a copy of the value is passed. >> If that element is a reference to an object, a copy of the object reference is passed.

Arrays as Parameters

>> An entire array can be passed as a parameter to a method. >> Because an array is an object, when an entire array is passed as a parameter, a copy of the reference (an alias) to the original array is passed. >> A method that receives an array as a parameter can permanently change an element of the array, because it is referring to the original element value. >> The method cannot permanently change the reference to the array itself, because a copy of the original reference is sent to the method.

Arrays of Objects

>> Arrays can also store references to objects as elements. >> Fairly complex information management structures can be created using only arrays and other objects.

Declaring an Array

>> In Java, arrays are objects. >> To create an array, the reference to the array must be declared: int[] height; >> The array can then be instantiated using the 'new' operator, which allocates memory space to store values: int[] height = new int[11]; >> Above creates an array of integers named 'height' with 11 elements, and an index range from 0 to 10.

Instantiating an Array of Objects

>> Instantiating an array of objects reserves room to store references only. >> The objects that are stored in each element must be instantiated separately. >> An array of objects is really an array of object references.

ArrayList

>> Resizable-array implementation of the List interface. >> ArrayLists cannot use primitive data types, such as int or double; MUST pass an object (instance of a class) to an ArrayList.

Bounds Checking

>> The index operator performs automatic bounds checking , which ensures that the index is in range for the array being referenced. >> Whenever a reference to an array element is made, the index must be greater than or equal to zero and less than the size of the array. int[] prices = new int[25] ; >> The above array has 25 elements. >> The valid indexes for the array above are from 0 to 24.

Index

>> The number corresponding to each position is called an index or a subscript. >> Array indexes always begin at zero.

Index operator

>> The square brackets used to indicate the index of an array are treated as an operator in Java. >> the index operator ( [] ) has a precedence relative to the other Java operators that determines when it is executed. >> It has the highest precedence of all Java operators.

initializer Lists

>> an initializer list is used to instantiate an array and provide the initial values for the elements of the array. char [] vowels = {'A', 'E', 'I', 'O', 'U'};

Two-Dimensional arrays

>>A two-dimensional array has values in two dimensions, which are often thought of as the rows and columns of a table. >> We must use two indexes to refer to a value in a two-dimensional array, one specifying the row and another the column. >> Nested for loops are used to create a 2-D array


Ensembles d'études connexes

NUR168: CONCEPTS 3: CHAPTER 8: COMMUNICATION:

View Set

Chapter 20: Heart Failure and Circulatory Shock Porth

View Set

Getting Started With Google Analytics 360, part 2

View Set

CH. 31 Functional Assessment of the Older Adult

View Set

Geometry 9 Lesson 5-5: Trapezoids

View Set

Python (Modern Python 3 Bootcamp, Sections 28-31, Decorators/Testing/FileIO/CSVs/Pickling)

View Set