score 배열의 각 인덱스는 각 항목의 데이터를 읽거나 저장하는데 사용되며 다음과 같이 배열 이름 옆에 대괄호 [ ]에 기입됩니다. 인덱스는 0부터 시작합니다.
score[0]=83;
score[1]=90;
score[2]=87;
.
.
1.타입[ ] 변수;
2.타입 변수[ ];
타입에는 데이터 자료형이 들어감.(String,double,int...)
배열 객체를 생성하려면 값 목록을 이용하거나 new 연산자를 이용하는 방법이 있습니다.
타입 변수[ ] = { 값0, 값1, 값2, 값3, … };
new 연산자로 배열 생성
값의 목록을 가지고 있지 않지만, 향후 값들을 저장할 배열을 미리 만들고 싶다면 new 연산자로 다음과 같이 배열 객체를 생성할 수 있습니다.
타입 변수[ ] = new 타입[길이];
예를 들어 배열 scores의 0, 1, 2 인덱스에 각각 83, 90, 75를 저장하는 코드는 다음과 같습니다.int[] scores = new int[3]; scores[0] = 83; scores[1] = 90; scores[2] = 75;
출처:링크텍스트
package com.human.ex;
public class Array {
public static void main(String[] args) {
int a[]= {53,6,85,3,5}; // 배열 a 생성
System.out.println(java.util.Arrays.toString(a)); // 배열 a의 내용 출력
}
}
package com.human.ex;
public class Array {
public static void main(String[] args) {
int a[]= {12,1,53,6,85,3};
int sum=0;
for(int i=0; i < a.length; i++) {
sum=sum+a[i];
}
System.out.println(sum);
}
}
package com.human.ex;
import java.util.Arrays;
public class Array {
public static void main(String[] args) {
int a[]= {1,2,3};
for(int i=0; i < a.length; i++) {
a[i]=a[i]+2;
}
System.out.println(java.util.Arrays.toString(a));
for(int i=a.length-1; i>=0; i--) {
System.out.print(a[i]);
}
}
}
package com.human.ex;
import java.util.Arrays;
public class Array {
public static void main(String[] args) {
int a[]= {1,2,3,4,5,6};
for(int i=0; i < a.length; i++) {
if(a[i]%2==0) {
System.out.println(a[i]);
}
}
}
}