The data type can be any of Java’s primitive types
Arrays:
? An array is a data structure that stores a sequence of values of the same type.
? The data type can be any of Java¡¯s primitive types:
? int, short, byte, long, float, double, boolean, char
? The data type can also be any class:
? String, SolidBoxes, etc.
? Each variable in the array is an element.
? An index specifies the position of each element in the array.
? Useful for many applications:
? Collecting statistics.
? Representing the state of a game.
? Etc.
1
Java Arrays vs Python Lists:
? The closest structure to an array in Python is the List, but there are many
differences.
? An array has a fixed size but the size of a Python List can change.
? All the elements of an array must be the same type, but a Python List can have
elements of many types.
? You may insert into the middle of a Python List, but not into an array
? You may concatenate a Python List, but not an array
? Why would we even have something like an array when a list is so much more
flexible?
? Answer: Nothing is free. Arrays are more efficient than lists.
2
Arrays
? Arrays are one of the oldest and most basic data structures in computer science.
Block of integers
? Many implementations of arrays
use a block of contiguous memory
in memory
3432
3
54
? It is the most efficient way to store a collection of
a known number of items.
-434
324
234
4
80948
? It also allows random access of items. (Through an index)
? In Java, arrays are objects so they contain more information, but the data is stored in
consecutive memory.
3
Declaring and Instantiating Arrays:
? Arrays are objects.
? Creating an array requires two steps:
int [] zapNumbers;
1. Declaring the reference to the array
zapNumbers = new int [ 173 ];
2. Instantiating the array.
float [] grades;
grades = new float[ 22 ] ;
String [] names;
names = new String[ 20 ];
? To declare a reference to the array:
datatype []
arrayName;
? To instantiate an array:
arrayName = new datatype[ size ];
? size is an int and is the number of elements that will be in the array.
4
? Examples:
? Declaring and instantiating arrays of primitive types:
double []
dailyTemps;
// elements are doubles
dailyTemps = new double[ 365 ];
boolean [] answers;
// 365 elements
// elements are booleans: true, false
answers = new boolean[ 20 ];
int [] cs127A, cs252;
// 20 elements
// two arrays, each containing integers
cs127A = new int[ 310 ];
// 310 elements for cs127A
cs252 = new int[ 108 ];
// 108 elements for cs252
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- declaring a dictionary in python
- declaring list in python functions
- declaring an int python
- declaring list in class in python
- declaring variables in python class
- the python guide for beginners
- primitive data types
- the data type can be any of java s primitive types
- declaring multiple global variables in python
Related searches
- 4 types of author s purpose
- 3 types of author s purpose
- if you could be any animal
- what percentage of wages can be garnished
- change data type of column pandas
- python data type of variable
- the semicircle from the scale drawing will be cut in full size from a piece of f
- can abiotic factors be composed of cells
- pandas change data type of column
- python check data type of variable
- python print data type of variable
- data type of pandas dataframe