您的位置:首页 > 其它

4、数组

2016-07-05 22:07 218 查看
一.数组

(1)定义数组
语法:数据类型[] 数组名;
数据类型 数组名[];

(2)指定数组大小:
语法:数组名=new 数据类型[size];

(3)给数组赋值
语法:数组名[index]=值;
注意:数组index索引下标从0开始

(4)取出数组的某个值
语法:数组名[index];

(5)数组的属性
语法:数组名.length;
作用:求得数组的长度

二、示例

2.1 示例:简单定义数组并赋值

package com.test;

public class Test {
public static void main(String[] args) {
//1.定义数组
int[] score;

//2.给数组指定大小
score=new int[5];

//3.给数组赋值
score[0]=15;
score[1]=26;
score[2]=36;
score[3]=46;

//4.取出数组中的值
int num=score[1];
System.out.println(num);

}

}


2.2 示例:循环给数组赋值和取值

package com.test;

import java.util.Scanner;

public class Test1 {

/**
* @param args
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请指定数组的大小:");
int size=input.nextInt();

//1.定义并确定数组大小
int[] score=new int[size];
//通过数组名.length获取数组的空间大小
System.out.println("数组的空间大小:"+score.length);

//2.循环给数组赋值
for (int i = 0; i < score.length; i++) {
System.out.print("请输入第:"+(i+1)+"个值");
score[i]=input.nextInt();
//score[i]=(int) (Math.random()*10);
System.out.println("完成第"+(i+1)+"次赋值");
}

//3.循环取值
for (int i = 0; i < score.length; i++) {
System.out.println("第"+i+"位置上的值为:"+score[i]);
}

}

}


2.3 示例:用foreach循环打印数组

package com.test;

import java.util.Scanner;

public class Test2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int num[]=new int[]{8,4,54,36,35,8};
int sum=0;
//for(数组的数据类型或集合的数据类型  循环增量:数组或集合的名称)
for (int i : num) {
System.out.println(i);
}

/*System.out.println("请输入一个数字");
int shuzi=input.nextInt();*/

/*for(int i=0;i<num.length;i++){
if(num[i]==shuzi){
System.out.println("对应的索引下标:"+i);
System.out.println("数组中包含此数为"+num[i]);
}
sum+=num[i];
}
*/

System.out.println("数组的和是:"+sum);
}

}


2.4 示例:请数组最大值

package com.test;
/**
* 求最大值
* @author Dell
*
*/
public class Test {
public static void main(String[] args) {
int[] score=new int[5];
score[0]=23;
score[1]=20;
score[2]=2;
score[3]=30;
score[2]=18;
int max=score[0];
for (int i = 1; i < score.length; i++) {
if(score[i]>max){
max=score[i];
}
}
System.out.println("最大值为:"+max);
}

}


2.5 示例:求数组最小值

package com.test;
/**
* 求最小值
* @author Dell
*
*/
public class Test1 {
public static void main(String[] args) {
int[] score=new int[5];
score[0]=99;
score[1]=100;
score[2]=82;
score[3]=63;
score[4]=88;

int min=score[0];
for (int i = 1; i < score.length; i++) {
if(min>score[i]){
min=score[i];
}
}
System.out.println("最大值为:"+min);
}
}


2.6 示例: 3.在原有降序数组中插入值,完后还是降序

package com.test;
/**
* 插入一个值,插入完之后是降序
* @author Dell
*
*/
public class Test3 {
public static void main(String[] args) {
int[] score=new int[6];
score[0]=99;
score[1]=98;
score[2]=97;
score[3]=96;
score[4]=18;
int index=0;//定义要插入的所有位置
int num=6; //要插入的值
//确定要插入的位置
for (int i = 0; i < score.length; i++) {
if(num>score[i]){
index=i;
break;
}
}

//循环将元素后移
for (int j = score.length-1; j >index; j--) {
score[j]=score[j-1];
}

//插入元素
score[index]=num;

//插入完成之后
for (int k = 0; k < score.length; k++) {
System.out.println(score[k]);
}
}

}


2.7 示例:冒号排序:从小到大

package com.test;

public class Test4 {

/**
* 冒号排序:从小到大排列
*/
public static void main(String[] args) {
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp=0;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}

}


2.8 冒号排序:从大到小

package com.test;

public class Test5 {

/**
* 冒号排序:从大到小排列
*/
public static void main(String[] args) {
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp=0;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1-i;j++){
if(a[j]<a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}

}


2.9 示例 :Arrays类的使用

package com.test;

import java.util.Arrays;

/**
* Arrays类的使用
* @author pc
*
*/
public class Test6 {
public static void main(String[] args) {
int[] score=new int[5];
score[0]=99;
score[1]=16;
score[2]=97;
score[3]=88;
score[4]=60;
//完成数组升序排序
Arrays.sort(score);

for (int i : score) {
System.out.println(i);
}

//将数组变成字符串
System.out.println(Arrays.toString(score));

}

}


2.10 示例 :二维数组的使用

package com.test;

public class Test7 {

/**
* @param args
*/
public static void main(String[] args) {
int[][] xy=new int[2][3];
xy[0][0]=90;
xy[0][1]=70;
xy[0][2]=50;
xy[1][0]=80;
xy[1][1]=80;
xy[1][2]=40;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: