W3schools - Array
이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Java Arrays
Arrays are used to store multiple values in a single variable
Declare an array
- (define the variable type)(square barckets) (variable name);
String[] cars;
Insert value to it
String[] cars = {“Volvo”, “BMW”};
- can use an array literal (place the values in a comma separated list, inside curly braces)
Access the Elements of an Array
-
access an array element by referring to the index number
-
array indexes start with 0
cars[0]; ==> Volvo
Change an Array Element
- To Change the value of a specific element, refer to the index number
cars[0] = “Opel”;
Array length
- find out how many elements an array has, use the length property
cars.length ==> 2
Loop Through an Array
Use for loop, use .length, can loop through the array elements
Loop Through an Array with For-Each
exclusively to loop through elements in array
for(type variable : arrayName) {
code block
}
Multidimentional Arrays
Is an array of arrays
int[][] myNumbers = {1, 2, {3,4}, {5,6,7}};
//each array within its own set of curly braces
int x = myNumbers[1][2]; ==> 7
//access the third element in the second array of myNumbers
Can also use a for loop inside another for loop to get the elements of a two-dimentional array
for(int i = 0; i < myNumbers.length; ++i){
for(int j = 0; j < myNumbers[i].length; ++j){
System.out.println(myNumbers[i][j];
}
}