배열의 활용
- 총합, 평균
- 최대, 최소값
- 섞기
- 임의 값으로 채우기
- 정렬
- 빈도수
int[] randArray = new int[10];
for (int i=0; i<randArray.length; i++){
randArray[i] = i;
}
System.out.println(Arrays.toString(randArray));
for(int i=0; i<randArray.length; i++){
int rand = (int)(Math.random()*10);
int tmp = randArray[i];
randArray[i] = randArray[rand];
randArray[rand] = tmp;
}
System.out.println(Arrays.toString(randArray));
int[] nums = new int[10];
int[] newNums = new int[10];
for(int i=0;i<nums.length;i++){
nums[i] = (int)(Math.random() * 30);
}
System.out.println(Arrays.toString(nums));
boolean done = false;
while(!done){
int toggle = 0;
for(int i=0; i < nums.length - 1; i++){
if (nums[i] > nums[i+1]) {
int tmp = nums[i];
nums[i] = nums[i+1];
nums[i+1] = tmp;
toggle = 1;
}
}
if (toggle==0) done=true;
}
System.out.println(Arrays.toString(nums));
}
int[] nums = new int[10];
for (int i = 0; i < nums.length; i++) {
nums[i] = (int) (Math.random() * 30);
}
System.out.println(Arrays.toString(nums));
for (int i=0; i<nums.length-1; i++){
boolean changed = false;
for (int j=0; j<nums.length-1-i;j++){
if (nums[j]>nums[j+1]){
int tmp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = tmp;
changed = true;
}
}
if (!changed) break;
}
System.out.println(Arrays.toString(nums));