JAVAサンプル5)配列---一次元配列
1 D配列
例55 1 D配列の作成と使用
インスタンス56 逆の順序で出力
インスタンス57 パリティグループ
インスタンス58 宝探し
インスタンス59 さいしょうすうをさがす
例60 私の位置はどこですか.
例61 配列のコピー
例62 新しい要素を挿入
例63 配列の結合
インスタンス64 重複要素の除去
例65 はいれつごうけいさん
例66 最大値、最小値と平均値を求めます
例55 1 D配列の作成と使用
import java.util.Random;
public class OneArray_01 {
public static void main(String[] args) {
Random rd = new Random(); // Random
int array[] = new int[10]; // 10 int array
System.out.println("
array :
");
for (int i = 0; i < array.length; i++) { //
array[i] = rd.nextInt(10); // 0~10
System.out.print(" " + array[i] + " "); //
if ((i + 1) % 5 == 0) // 5
System.out.println();
}
}
}
インスタンス56 逆の順序で出力
public class OneArray_02 {
public static void main(String[] args) {
int[] row = { 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 };
System.out.println("
:");
for (int i = 0; i < row.length; i++) {
System.out.print(" " + row[i] + " ");
if ((i + 1) % 5 == 0)
System.out.println();
}
System.out.println("
:");
for (int i = row.length - 1; i >= 0; i--) {
System.out.print(" " + row[i] + " ");
if (i % 5 == 0)
System.out.println();
}
}
}
インスタンス57 パリティグループ
import java.util.Random;
public class OneArray_03 {
public static void main(String[] args) {
Random rd = new Random(); // Random
int total[] = new int[30]; // 30 int total( )
int odd[] = new int[30]; // 30 int odd( )
int even[] = new int[30]; // 30 int even( )
int j = 0, k = 0;
System.out.println(" total :");
for (int i = 0; i < total.length; i++) {
total[i] = rd.nextInt(100); // total
if (total[i] % 2 == 0) { //
even[k] = total[i];
k++;
} else {
odd[j] = total[i];
j++;
}
System.out.print(" " + total[i]);
if ((i + 1) % 5 == 0) // 5
System.out.println();
}
System.out.println("
:");
int max = j > k ? j : k; // j k
int min = j > k ? k : j; // j k
/* , */
for (int x = 0; x < max; x++) {
if (x >= min && j == min) { // x , ,
System.out.print(" " + even[x]);
} else if (x >= min && k == min) { // x , ,
System.out.print(" " + odd[x]);
} else if (x < min) { //
System.out.print(" " + odd[x] + "," + even[x]);
}
if ((x + 1) % 5 == 0) // 5
System.out.println();
}
}
}
インスタンス58 宝探し
import java.util.Random;
public class OneArray_04 {
public static void main(String[] args) {
Random rd = new Random(); // Random
int len = rd.nextInt(20); //
int[] box = new int[len]; // ( )
System.out.println(" " + len + " , :");
for (int i = 0; i < box.length; i++) {
box[i] = rd.nextInt(20); //
System.out.print(box[i] + " ");
if ((i + 1) % 5 == 0)
System.out.println();
}
System.out.println();
int index = searchBotey(box, 8); // searchBotey
if (index == -1) {
System.out.print(" ");
} else {
System.out.print(" " + (index + 1) + " ");
}
}
public static int searchBotey(int[] box, int index) {// box ,index
int num = -1;
for (int i = 0; i < box.length; i++) {
if (index == box[i]) {
num = i;
}
}
return num;
}
}
インスタンス59 さいしょうすうをさがす
public class OneArray_05 {
public static void main(String[] args) {
int array[] = { 12, 89, 51, 24, 5, 121, 63, 10, 23, 15 }; // array
int min = 0;
int flag = 0;
System.out.println(" :");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
if ((i + 1) % 5 == 0)
System.out.println();
}
for (int i = 0; i < array.length; i++) { //
if (i == 0) { // , min
min = array[0];
flag = 0;
} else {
// min , min , min
if (min > array[i]) {
min = array[i];
flag = i;
}
}
}
//
int n = array[flag];
array[flag] = array[array.length - 1];
array[array.length - 1] = n;
System.out.println("
:");
for (int i = 0; i < array.length; i++) { //
System.out.print(array[i] + " ");
if ((i + 1) % 5 == 0)
System.out.println();
}
System.out.println("
:" + min + " " + flag);
}
}
例60 私の位置はどこですか.
public class OneArray_06 {
public static void main(String[] args) {
int[] person = getLocation();
System.out.println(" :( ,0 ,1 )");
for (int i = 0; i < person.length; i++) {
System.out.print(person[i] + " ");
if ((i + 1) % 5 == 0)
System.out.println();
// 1, , ,
if (person[i] == 1) {
System.out.println(" :" + i);
}
}
}
public static int[] getLocation() {
int person[] = new int[17]; // 17
for (int i = 0; i < person.length; i++) { // 1
person[i] = 1;
}
int i = 0; //
int k = 0; //
int count = 0; //
while (true) {
i = i % 17; // i 0~16
if (person[i] == 1) { //
k++; // 1
if (k % 3 == 0) { // 3
person[i] = 0; // 0,
count++; // 1
}
}
if (count == 16) { // 16 ,
break;
}
i++;
}
return person;
}
}
例61 配列のコピー
public class OneArray_07 {
public static void main(String[] args) {
copy();
}
public static void copy() { //
int copy1[] = new int[] { 23, 12, 67, 89, 56, 47, 13 }; //
// copy1 ,
int copy2[] = new int[copy1.length];
System.out.println(" copy1 :");
for (int i = 0; i < copy1.length; i++) { // copy1
System.out.print(copy1[i] + " ");
if ((i + 1) % 4 == 0)
System.out.println();
}
System.arraycopy(copy1, 0, copy2, 0, copy1.length); // copy1 copy2
System.out.println("
copy2 :");
for (int i = 0; i < copy2.length; i++) { // copy2
System.out.print(copy2[i] + " ");
if ((i + 1) % 4 == 0)
System.out.println();
}
}
}
例62 新しい要素を挿入
import java.util.Scanner;
public class OneArray_08 {
public static void main(String[] args) {
insertArray ();
}
public static void insertArray () {
int i, j;
int n = 5;
int insertNum[] = new int[n + 1]; //
for (i = 0; i < insertNum.length - 1; i++) {
insertNum[i] = i + (1 + 2 * i); //
}
int length = insertNum.length; //
System.out.println(" :");
for (i = 0; i < length; i++) //
if (insertNum[i] == 0)
System.out.print("");
else
System.out.print(insertNum[i] + " ");
System.out.println();
System.out.println(" :");
Scanner scan = new Scanner(System.in); //
int in = scan.nextInt();
for (i = 0; i < length - 1; i++) { //
if (insertNum[i] > in)
break;
}
for (j = length - 1; j > i; j--) { // ,
insertNum[j] = insertNum[j - 1];
}
insertNum[i] = in; //
System.out.println("
" + in + " insertNum , :");
for (i = 0; i < length; i++) //
System.out.print(insertNum[i] + " ");
System.out.println();
}
}
例63 配列の結合
public class OneArray_09 {
public static void main(String[] args) {
int a[] = {1,3,5,7,9};
int b[] = {0,2,4,6,8};
int c[] = combineArray (a,b);
System.out.println(" c :");
for (int k = 0; k < a.length + b.length; k++) //
System.out.print(c[k]+" ");
System.out.println();
}
public static int[] combineArray (int[] a, int[] b) {
int al = a.length; // a
int bl = b.length; // b
int length = al + bl; //
int i, j;
System.out.println(" a :");
for(i=0;i<al;i++) // a
System.out.print(a[i]+" ");
System.out.println("
b :");
for(i=0;i<bl;i++) // b
System.out.print(b[i]+" ");
System.out.println();
int[] c = new int[length]; // c
for (i = 0, j = 0; i < al && j < bl;){ //
if (a[i] < b[j]) { //
c[i + j] = a[i];
i++;
} else {
c[i + j] = b[j];
j++;
}
}
if (i == al) // b j c , c al+j,bl-j
System.arraycopy(b, j, c, al + j, bl - j);
if (j == bl) // a i c , c bl+i,al-i
System.arraycopy(a, i, c, bl + i, al - i);
return c;
}
}
インスタンス64 重複要素の除去
import java.util.Arrays;
import java.util.Random;
public class OneArray_10 {
public static int[] trim(int array[]) {
int len = array.length; //
int[] brray = new int[len]; // ,
int newlen = len;
for (int i = 0; i < len; i++) { // brray
brray[i] = 0;
}
for (int j = 1; j < len; j++) { //
if (array[j] == array[j - 1]) {
brray[j] = 1;
newlen--;
}
}
int[] newArray = new int[newlen]; // , -
int newId = 0;
for (int i = 0; i < len; i++) { //
if (brray[i] == 0) { // 1
newArray[newId++] = array[i];
}
}
return newArray;
}
public static void main(String[] args) {
Random rd = new Random();
int[] array = new int[20]; //
System.out.println(" " + array.length + " , :");
for (int i = 0; i < array.length; i++) { // 20 0~20
array[i] = rd.nextInt(20); // array
}
Arrays.sort(array); // array
for (int i = 0; i < array.length; i++) { // array
System.out.print(" " + array[i]);
if ((i + 1) % 5 == 0)
System.out.println();
}
int c[] = trim(array); // trim
System.out.println("
, " + c.length + " , :");
for (int i = 0; i < c.length; i++) { //
System.out.print(c[i] + " ");
}
}
}
例65 はいれつごうけいさん
import java.util.Random;
public class OneArray_11 {
public static void main(String[] args) {
Random rd = new Random();
int[] array = new int[20]; //
System.out.println(" :");
for (int i = 0; i < array.length; i++) { // 20 0~20
array[i] = rd.nextInt(20); // array
System.out.print(" " + array[i]); // array
if ((i + 1) % 5 == 0) // 5
System.out.println();
}
int sum = sumArray(array); // sumArray , array
System.out.println("
array :" + sum);
}
public static int sumArray(int[] a) { //
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum = sum + a[i]; //
}
return sum;
}
}
例66 最大値、最小値と平均値を求めます
import java.util.Random;
public class OneArray_12 {
public static void main(String[] args) {
Random rd = new Random();
double a[] = new double[10]; // 10 double
System.out.println(" :");
for (int i = 0; i < a.length; i++) { //
a[i] = Math.rint(rd.nextDouble() * 100); // double double
System.out.print(" " + a[i]);
if ((i + 1) % 5 == 0)
System.out.println();
}
System.out.println();
double b[] = show(a); // show
System.out.println(" a max :" + b[0]); //
System.out.println(" a min :" + b[1]); //
System.out.println(" a ave :" + b[2]); //
}
public static double[] show(double[] dou) { // 、
double[] result = new double[3]; // 3 double
double max = dou[0], min = dou[0], sum = dou[0];
for (int i = 1; i < dou.length; i++) {
max = max > dou[i] ? max : dou[i]; //
min = min < dou[i] ? min : dou[i] ; //
sum += dou[i]; //
}
result[0] = max;
result[1] = min;
result[2] = sum / dou.length; //
return result;
}
}