- 編寫一個(gè)簡(jiǎn)單程序,要求數(shù)組長(zhǎng)度為5牛欢,分別賦值10骡男,20,30傍睹,40隔盛,50,在控制臺(tái)輸出該數(shù)組的值拾稳。
/*例5-1
*數(shù)組使用范例
*/
public class ArrayDemo
{
public static void main(String[] args)
{
int[] buffer=new int[5];
buffer[0]=10;
buffer[1]=20;
buffer[2]=30;
buffer[3]=40;
buffer[4]=50;
for(int i=0;i<5;i++)
{
System.out.println(buffer[i]);
}
}
}
- 將一個(gè)字符數(shù)組的值(neusofteducation)考貝到另一個(gè)字符數(shù)組中吮炕。
public class ArrayCopyDemo {
public static void main(String[ ] args) {
//定義源字符數(shù)組
char[ ] copyFrom = {'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n'};
char[ ] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
- 給定一個(gè)有9個(gè)整數(shù)(1,6,2,3,9,4,5,7,8})的數(shù)組,先排序访得,然后輸出排序后的數(shù)組的值龙亲。
public class ArraySortDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] point = {1,6,2,3,9,4,5,7,8};
java.util.Arrays.sort( point );
for(int i=0;i<point.length;i++)
{
System.out.println(point[i]);
}
}
}
- 有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列是21+35+42
第1行2列是25+39+47 第2行1列是41+65+8*2 依次類推俱笛。
package com.neusoft.javaTest;
public class Array2 {
/**
* @param args
*/
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 k=0;k<a.length;k++){
for(int i=0;i<b[0].length;i++){
int num = 0;
for(int j=0;j<b.length;j++){
num += a[k][j]*b[j][i];
}
System.out.print(num+" ");
}
System.out.println("");
}
}
}
- 輸出一個(gè)double型二維數(shù)組(長(zhǎng)度分別為5、4传趾,值自己設(shè)定)的值迎膜。
/*例5-3
*多維數(shù)組范例
*/
public class ArrayTwoDimension
{
public static void main(String[] args)
{
double[][] buffer=new double[5][4];
for(int i=0;i<buffer.length;i++)
{
for(int j=0;j<buffer[0].length;j++)
{
System.out.print(buffer[i][j]);
}
System.out.println();
}
}
}
- 在一個(gè)有8個(gè)整數(shù)(18,25浆兰,7磕仅,36,13簸呈,2榕订,89,63)的數(shù)組中找出其中最大的數(shù)及其下標(biāo)蜕便。
public class Arraymax {
/**
* @param args
*/
public static void main(String[] args) {
int[] a = {18,25,7,36,13,2,89,63};
int max = a[0];
int maxIndex = 0;
for(int i=1;i<a.length;i++)
{
if(max<=a[i]){
max = a[i];
maxIndex = i;
}
}
System.out.println("最大值為:"+max+" 最大值下標(biāo)為:"+maxIndex);
}
}
public class Answer {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a[] = new int[20];
System.out.println("請(qǐng)輸入多個(gè)正整數(shù)(輸入-1表示結(jié)束):");
int i=0,j;
do{
a[i]=s.nextInt();
i++;
}while (a[i-1]!=-1);
System.out.println("你輸入的數(shù)組為:");
for( j=0; j<i-1; j++) {
System.out.print(a[j]+" ");
}
System.out.println("\n數(shù)組逆序輸出為:");
for( j=i-2; j>=0; j=j-1) {
System.out.print(a[j]+" ");
}
}
}
- 將一個(gè)數(shù)組中的重復(fù)元素保留一個(gè)其他的清零劫恒。
public class Answer {
public static void main(String[] args) {
int[] a = { 1, 2, 2, 3, 4, 5, 6, 4, 7 ,2 ,10};
for (int i = 0;i < a.length - 1;i ++){
for (int j = i + 1;j < a.length;j ++){
if (a[i] == a[j]){
a[j] = 0;
}
}
}
}
}
- 給定一維數(shù)組{ -10,2轿腺,3两嘴,246,-100族壳,0憔辫,5} ,計(jì)算出數(shù)組中的平均值仿荆、最大值贰您、最小值坏平。
public class Answer {
public static void main(String[] args) {
int a[] = new int[]{ -10,23,246,-100,0,5};
int max = a[0];
int min = a[0];
int add = a[0];
for(int i =1;i<a.length;i++){
if(a[i]< min){
min = a[i];
}else if(a[i]>max){
max = a[i];
}
add = add+a[i];
}
System.out.println("最小值:"+min);
System.out.println("最大值:"+max);
System.out.println("平均值:"+add/a.length);
}
}