JAVA_learning15

数组的输出

遍历输出数组的元素
public class Test{
	public static void main(String args[]){
		int[] a = {2,4,6,7,3,5,9,1,8};
		for(int i =0; i < a.length;i++) {
			System.out.println( a[i] );
		} 
	}
}

输出结果
在这里插入图片描述

输出命令行参数
public class Test{
	public static void main(String args[]){
		int[] a = {2,4,6,7,3,5,9,1,8};
		for (int i = 0; i < args.length; i++){
			System.out.print(args[i] + " ");
		}
	}
}

该程序输出的是命令行参数的内容,因此若指令为 java Test,不添加具体参数则不会产生具体输出
图1
接上命令行参数才会输出
在这里插入图片描述
也就说明,args这个数组会存放命令行参数

public class Test{
	public static void main(String args[]){
		int[] a = {2,4,6,7,3,5,9,1,8};
		System.out.print(args.length );
		for (int i = 0; i < args.length; i++){
			System.out.print(args[i] + "1" );
		}
	}
}

args.length,即args这个数组存放的参数的个数(用空格隔开每个参数)
在这里插入图片描述

args中命令行参数的使用
public class Test{
	public static void main(String args[]){
		if (args.length < 3){
			System.out.println("Usage should be java Test \"n1\" \"op\" \"n2\" ");
			//双引号不能直接输出,前面需加上转义字符\
			System.exit(-1);
		}
		double d1 = Double.parseDouble(args[0]);
		double d2 = Double.parseDouble(args[2]);
		double d = 0;
		if (args[1].equals("+")) d = d1 + d2;
		else if (args[1].equals("-")) d= d1 - d2;
		else if (args[1].equals("x")) d = d1 * d2;
		else if (args[1].equals("/")) d = d1 / d2;
		else{
			System.out.println("Error operator");
			System.exit(-1);
		}
		System.out.println(d);
	}
}
打印args中命令行参数,并排序

练习:
定义了一个数组 a=[1,2,312,3,1231,345345,6434],要求将这个数组的数按从小到大输出
1

public class NumSort{
	public static void main(String args[]){
		int a[] = new int[args.length];
		for (int i = 0; i < args.length; i++){
			a[i] = Integer.parseInt(args[i]);
			//把给的数组中的数取出来,并且将其由字符型转化为整型
		}
		//print(a);
		sort(a);
		print(a);
	}
	private static void print(int[] a){//写一个方法,用来打印一个int类型的数组
		for (int i =0; i<a.length;i++){
			System.out.println(a[i]);
		}
	}
	private static void sort(int[] a){//写一个方法,用来对一个int类型的数组里的元素大小排序
		int b=0;
		for (int i =0; i<a.length;i++){
			for (int j =i+1; j<a.length;j++){
				if (a[j]<a[i]){
					b=a[j];
					a[j]=a[i];
					a[i]=b;
				}
			
			}
		}	
	}
}

2

public class Test{
	public static void main(String args[]){
		int[] a = new int[args.length];
		for (int i = 0; i<args.length;i++){
			a[i] = Integer.parseInt(args[i]);
		}
		//print(a);
		selectionSort2(a);
		print(a);
		
	}

	private static void selectionSort1(int[] a){
		for (int i = 0; i<a.length;i++){
			for (int j = i+1; j<a.length;j++){
				if (a[j] < a[i]){
					int temp = a[i];
					a[i] = a[j];
					a[j] = temp;	
				}
			}
		}
	}



	//在1的基础上优化,数与数之间只会调换一次
	private static void selectionSort2(int[] a){
		for (int i = 0; i<a.length;i++){
			int k=i;//当前数的序号
			for (int j = k+1; j<a.length;j++){
				if (a[j] < a[k]){
					k=j;	//获取较小的数的序号
				}
			}
			if(k != i){
				int temp = a[i];
				a[i] = a[k];
				a[k] = temp;
			}
		}
	}

	//在2的基础上优化,k与temp无需反复定义
	private static void selectionSort3(int[] a){
		int k, temp;//在循环外定义,则无需反复定义
		for (int i = 0; i<a.length;i++){
			k=i;
			for (int j = k+1; j<a.length;j++){
				if (a[j] < a[k]){
					k=j;	
				}
			}
			if(k != i){
				temp = a[i];
				a[i] = a[k];
				a[k] = temp;
			}
		}
	}

	private static void print(int[] a){
		for ( int i=0;i<a.length;i++){
			System.out.println(a[i]);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值