Array
Understanding Arrays
What is an Array?
A way to handle multiple variables of the same type as a single group
An array is a data type that allows handling memory spaces of the same type under a single name.
A variable accompanied by a position index
=> A position index always accompanies it
Since arrays are also Classes, methods can be used
Once the size of an array is set, it cannot be resized!
Disadvantages of Arrays
The
sizecannot be changedIf expansion is needed, a new array must be created and copied
If the array is created with a large size to improve execution speed, memory is wasted
Adding/removingnon-sequential datatakes a lot of timeAdding data sequentially and removing the last data is very fast, but
Adding/removing values in the middle of the array takes a long time
Array vs ArrayList & LinkedList
Arrays have these disadvantages, and
ArrayList, whichimplements List using arrays, also has the same problemsThe data structure designed to address these issues is
LinkedListWhile arrays have all data existing
contiguously,LinkedListconsists ofnon-contiguousdata connected to each other
Using Arrays
1. Declaration
type[] variableName;
ex)
2. Creation
variableName = new type[length];
ex)
-> Array declaration and creation done at once
* The length of an array must be a positive integer (including 0) within the int range!
3. Initialization
: Since arrays are automatically initialized to the default value of their type upon creation, you don't need to initialize them separately before use, but to store desired values, you must assign values to each element
ex)
Create a memory space called num in the Stack area
And integers will be stored here
The moment an array is declared, it becomes a reference type!!!
Create an address called nums in the Stack area
Since it's new, create 5 rooms of int type in the Heap area
Since it's in the heap, JVM manages it
Then, each of the 5 rooms undergoes default initialization to 0 since they are int type, by the JVM
Once created this way, resizing is not possible
Guaranteed to receive contiguous memory allocation
What is the size of the nums array?
Create a memory space called String in the Stack area
And addresses will be stored here
reference type!
Each of the 5 rooms undergoes default initialization to null by the JVM
The address pointing to "Hong Gildong" in the code table area is stored in room 0 of names
4. Copying Arrays
Parameters :
source_arr : array to be copied from (original array)
sourcePos : starting position in source array from where to copy
dest_arr : array to be copied in (destination array)
destPos : starting position in destination array, where to copy in
length : total no. of components to be copied. (size of the original array)
5. Two-Dimensional Array
An array made by collecting addresses of one-dimensional arrays
A two-dimensional array stores the addresses of data pointed to by one-dimensional arrays
When an array is created, each element is automatically stored with the default value of the element type
Since a 2D array consists of
rowsandcolumns, indexes exist for both rows and columnsRow index range = 0 to row length - 1
Column index range = 0 to column length - 1
Length of a Two-Dimensional Array
String to char array
: .toCharArray();
(String ...) vs String[]
(String ...) means you can add as much String param as you want
String[] is one parameter which is an array of strings.
Running args in Oracle
Last updated