1.1.5 300~9之间的数组,分别统计0~910个数组分别出现了多少次。

class test408 {

// 有30个0~9之间的数组,分别统计0~9这10个数组分别出现了多少次。

public static void main(String args[]) {

int i[] = { 0, 0, 9, 8, 7, 6, 6, 7, 8, 8, 5, 4, 3, 4, 5, 6, 6, 3, 2, 2,

1 };

// 定义一个数组,用来统计0~9的数字个数

int count[] = new int[10];

for (int x = 0; x < i.length; x++) {

switch (i[x]) {

case 0: {

count[0]++;

break;

}

case 1: {

count[1]++;

break;

}

case 2: {

count[2]++;

break;

}

case 3: {

count[3]++;

break;

}

case 4: {

count[4]++;

break;

}

case 5: {

count[5]++;

break;

}

case 6: {

count[6]++;

break;

}

case 7: {

count[7]++;

break;

}

case 8: {

count[8]++;

break;

}

case 9: {

count[9]++;

break;

}

}

}

int temp = 0;

for (int x = 0; x < count.length; x++) {

System.out.println(temp + "的出现次数:" + count[x]);

temp++;

}

}

}

1.1.6 定义一个整型数组,保存10个数据,利用程序完成将最大值保存在数组中第1个元素的操作。

class test409 {

public static void main(String args[]) {

int i[] = { 1, 2, 3, 54, 5, 67, 8, 0 };

// 求出最大值的位置

int max = i[0];

// 表示现在最大值的下标

int max_foot = 0;

for (int x = 0; x < i.length; x++) {

if (max < i[x]) {

max = i[x];

max_foot = x;

}

}

int temp = i[0];

i[0] = i[max_foot];

i[max_foot] = temp;

for (int x = 0; x < i.length; x++) {

System.out.print(i[x] + "\t");

}

}

}

1.1.7 在排序好的数组中添加一个数字,将添加后的数字插入到数组合适的位置。

class test410 {

public static void main(String args[]) {

int i[] = new int[10];

i[0] = 2;

i[1] = 1;

i[2] = 43;

i[3] = 23;

i[4] = 2;

i[5] = 56;

i[6] = 7;

i[7] = 8;

i[8] = 9;

java.util.Arrays.sort(i);

i[9] = 10;

java.util.Arrays.sort(i);

for (int x = 0; x < i.length; x++) {

System.out.print(i[x] + "\t");

}

}

}