/*
* 1毕谴、編寫(xiě)一個(gè)簡(jiǎn)單程序成畦,要求數(shù)組長(zhǎng)度為5距芬,分別賦值10,20羡鸥,30蔑穴,40忠寻,50惧浴,
* 在控制臺(tái)輸出該數(shù)組的值。(知識(shí)點(diǎn):數(shù)組定義和創(chuàng)建奕剃、一維數(shù)組初始化)*/
public class Arrayses {
public static void main(String[] args) {
int[] arr = new int[] { 10, 20, 30, 40, 50 };
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " ");
}
}
}
運(yùn)行圖:
/*
* 2衷旅、將一個(gè)字符數(shù)組的值(education)拷貝到另一個(gè)字符數(shù)組中。*/
public class ArraysCopy {
public static void main(String[] args) {
char[] c = new char[] { 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n' };
char[] a = new char[9];
for (int i = 0; i < c.length; i++) {
a[i] = c[i];
}
System.out.println(a);
}
}
運(yùn)行圖:
/*
* 3纵朋、給定一個(gè)有9個(gè)整數(shù)(1,6,2,3,9,4,5,7,8)的數(shù)組柿顶,
* 先排序,然后輸出排序后的數(shù)組的值
* Arrays.sort排序操软、冒泡排序
*/
public class Bulble {
public static void main(String[] args) {
int[] a = { 1, 2, 8, 6, 4, 9, 3, 4, 6, 4 };
// Arrays.sort()
/*
* java.util.Arrays.sort(a); for(int element : a) {//增強(qiáng)
* System.out.print(element+" "s); }
*/
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int element : a) {// 增強(qiáng)
System.out.print(element + " ");
}
}
}
運(yùn)行圖:
/*
* 4嘁锯、有2個(gè)多維數(shù)組分別是 2 3 4? 和? 1 5 2 8
? 4 6 8? ? 5 9 10 -3
? ? ? ? ? ? 2 7 -5 -18
? 按照如下方式進(jìn)行運(yùn)算。生成一個(gè)2行4列的數(shù)組聂薪。
? 此數(shù)組的第1行1列是2*1+3*5+4*2第1行2列是2*5+3*9+4*7?
? 第2行1列是4*1+6*5+8*2 依次類(lèi)推家乘。(知識(shí)點(diǎn):多維數(shù)組定義和創(chuàng)建、數(shù)組遍歷藏澳、數(shù)組元素訪問(wèn))
*/
public class Arrayser {
public static void main(String[] args) {
int a[][] = { { 2, 3, 4 }, { 4, 6, 8 } };
int b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[0].length; j++) {
int num = 0;
for (int k = 0; k < b.length; k++) {
num += a[i][k] * b[k][j];
}
System.out.print(num + " ");
}
System.out.println("");
}
}
}
運(yùn)行圖:
/*
* 5仁锯、 輸出一個(gè)double型二維數(shù)組(長(zhǎng)度分別為5、4翔悠,
* 值自己設(shè)定)的值业崖。(知識(shí)點(diǎn):數(shù)組定義和創(chuàng)建、多維數(shù)組初始化蓄愁、數(shù)組遍歷)
*/
public class DoubleArray {
public static void main(String[] args) {
double[][] a = { { 1, 2, 3, 4 }, { 5, 4, 3, 2 }, { 6, 5, 8, 7 }, { 6, 9, 4, 2 }, { 5, 8, 6, 1 } };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
運(yùn)行圖:
/*
* 6双炕、 在一個(gè)有8個(gè)整數(shù)(18,25撮抓,7雄家,36,13胀滚,2趟济,89,63)的數(shù)組中
* 找出其中最大的數(shù)及其下標(biāo)咽笼。(知識(shí)點(diǎn):數(shù)組遍歷顷编、數(shù)組元素訪問(wèn))
*/
public class FindArray {
public static void main(String[] args) {
int[] a = { 18, 25, 7, 36, 13, 2, 89, 63 };
int max = a[0];
int maxidx = 0;
for (int i = 1; i < a.length; i++) {
if (max <= a[i]) {
max = a[i];
maxidx = i;
}
}
System.out.println("最大值:" + max + "最大值下標(biāo):" + maxidx);
}
}
運(yùn)行圖:
/*8.將一個(gè)數(shù)組中的重復(fù)元素保留一個(gè)其他的清零。(知識(shí)點(diǎn):數(shù)組遍歷剑刑、數(shù)組元素訪問(wèn))*/
import java.util.Arrays;
public class ClearArray {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 4, 7, 2, 10 };
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
arr[j] = 0;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
運(yùn)行圖:
/*9媳纬、給定一維數(shù)組{ -10双肤,2,3钮惠,246茅糜,-100,0素挽,5} 蔑赘,計(jì)算出數(shù)組中的平均值、最大值预明、最小值缩赛。(知識(shí)點(diǎn):數(shù)組遍歷、數(shù)組元素訪問(wèn))*/
public class CalculateArray {
public static void main(String[] args) {
int arr[] = new int[] { -10, 23, 246, -100, 0, 5 };
int max = arr[0];
int min = arr[0];
int add = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
} else if (arr[i] > max) {
max = arr[i];
}
add = add + arr[i];
}
System.out.println("最小值:" + min);
System.out.println("最大值:" + max);
System.out.println("平均值:" + add / arr.length);
}
}
運(yùn)行圖: