一维数组的开辟 赋值 应用

package 第十五个工程;
import java.util.Scanner;
public class Domo15 
{
	public static void main(String args[])
	{
		//System.out.println("\u03b1 \u03b2 \u03b3");
		final int M = 100;
		//开辟一个长度为100的数组
		double[] numbers = new double[M];
		double sum = 0;
		
		for(int i = 0;i < M;i ++)
		{
			//随机生成0~100的浮点数
			numbers[i] = Math.random() * 100;
			System.out.print(numbers[i] + " ");
			sum += numbers[i];
		}
		System.out.println();
		//求平均值
		double aver = sum / M;
		System.out.printf("%.2f\n", aver);
		System.out.println();
		
		//声明,创建,并初始化一个长度为10的数组
		int[] a = {0,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2};
		//两种输出方法
		for(int i : a)
			System.out.print(i + " ");
		System.out.println();
		
		for(int i = 0;i < a.length;i ++)
			System.out.print(a[i] + " ");
		System.out.println();
		
		//字符串数组可以用一条打印语句全部输出
		char[] str = {'C','h','i','n','a'};
		System.out.println(str);
	}
}

package 第十三个工程;
import java.util.*;
public class Domo13 
{
	public static void sort_1(int []a)
	{
		for(int i = 0;i < a.length;i ++)
		{
			for(int j = 0;j < a.length;j ++)
				if(a[j] > a[i])
				{
					int t = a[j];
					a[j] = a[i];
					a[i] = t;
				}
		}
	}
	public static int[] sort_2(int []a)//注意变量的定义类型
	{
		for(int i = 0;i < a.length;i ++)
		{
			for(int j = 0;j < a.length;j ++)
				if(a[j] > a[i])
				{
					int t = a[j];
					a[j] = a[i];
					a[i] = t;
				}
		}
		return a;
	}
	public static void print_a(int []a,String str)
	{
		System.out.println(str);
		for(int i:a)
			System.out.print(i + " ");
		System.out.println();
	}
	public static void change(int in,int []a)
	{
		in += 1;
		System.out.println(in);
		a[0] = 0;
		a[1] = 0;
		a[2] = 0;
	}
	public static void print(int []a)
	{
		for(int i:a)
		{
			System.out.print(i + " ");
		}
		System.out.println();
	}
	public static void print_a(int in,int []a)
	{
		System.out.println("in = " + in);
		System.out.print("a :");
		print(a);
	}
	public static void main(String args[])
	{
		int []a = {1,4,7,2,5,8,3,6,9,0};
		int []cc;
		cc = a;
		print_a(a,"排序前");
		Arrays.sort(a);
		print_a(a,"排序后");
		int in = 10;
		int []b;
		print_a(cc,"------");	//证明a数组的值变化同样会引起cc数组的变化
		a = cc;
		print_a(a,"排序前");
		sort_1(a);
		print_a(a,"排序后");
		b = sort_2(a);///数组传递
		print_a(b,"数组变化后");
		
		int[] c;
		c = a.clone();///数组克隆,克隆的数组值发生变化不会引起原有数组的值变化
		c[0] = 10;
		print_a(c,"克隆的数组");
		
		if(c == a)//判断二者的地址是否相同
			System.out.println("两个数组的地址相同");
		else
			System.out.println("两个数组的地址不同");
		int []d = a;
		if(d == a)
			System.out.println("两个数组的地址相同");
		else
			System.out.println("两个数组的地址不同");
		System.out.println("调用change之前 ");
		print_a(in,a);
		
		change(in,a);
		System.out.println("调用change以后");
		print_a(in,a);//形参和实参没有关系,所以形参的值变化不会引起实参的值变化
	}
}


package 第十三个工程;
public class Domo13 
{
	//System.arraycopy进行两个数组复制的时候内存发生变化,改变一个的值另一个不会改变
	//a.clone()进行两个数组复制的时候内存发生变化,改变一个的值另一个会改变
	//d == a进行两个数组复制的时候内存不会发生变化,改变一个的值另一个也会改变
	//使用循环语句逐个的复制数组元素,内存地址不会发生变化
	public static void main(String args[])
	{
		char s1[]={'a','b','c'}; 
		char s2[]={'c','b','a'}; 
		char s3[]=new char[s1.length+s2.length]; 
		System.arraycopy(s1,0,s3,0,s1.length); 
		System.arraycopy(s2,0,s3,s1.length,s2.length); 
		System.out.println(s3);
		
		int []a = {1,4,7,2,5,8,3,6,9,0};
		
		int []b = new int[a.length];//注意要复制到的数组要开辟内存
		System.arraycopy(a,0,b,0,a.length);
		a[0] = 100;
		for(int i : b)
			System.out.print(i + " ");
		System.out.println();
		if(b == a)  
            System.out.println("两个数组的地址相同");  
        else  
            System.out.println("两个数组的地址不同");
		
		int[] c;  
        c = a.clone();///数组克隆,克隆的数组值发生变化不会引起原有数组的值变化  
        a[0] = 80;
        for(int i : c)
			System.out.print(i + " ");
		System.out.println();
        
        if(c == a)//判断二者的地址是否相同  
            System.out.println("两个数组的地址相同");  
        else  
            System.out.println("两个数组的地址不同");
        
        int[] d = a;
        a[0] = 60;
        for(int i : d)
			System.out.print(i + " ");
		System.out.println();
        if(d == a)  
            System.out.println("两个数组的地址相同");  
        else  
            System.out.println("两个数组的地址不同");
        
        int[] e = new int[a.length];
        for(int i = 0;i < a.length;i ++)
        	e[i] = a[i];
        for(int i : e)
        	System.out.print(i + " ");
		System.out.println();
		if(e == a)  
            System.out.println("两个数组的地址相同");  
        else  
            System.out.println("两个数组的地址不同");
        /*
程序输出
abccba
1 4 7 2 5 8 3 6 9 0 
两个数组的地址不同
100 4 7 2 5 8 3 6 9 0 
两个数组的地址不同
60 4 7 2 5 8 3 6 9 0 
两个数组的地址相同
60 4 7 2 5 8 3 6 9 0 
两个数组的地址不同
         */
	}
}

package 第五个工程;

public class Domo5 
{
	public static void main(String args[])
	{
		//new动态开辟的两个内存空间,所在存储位置不同
		String s1 = new String("java");
		String s2 = new String("java");
		String s3 = s2;
		if(s1 == s2)//比较的是内存地址
			System.out.println("s1 == s2");
		else
			System.out.println("s1 != s2");
		if(s2 == s3)
			System.out.println("s2 == s3");
		else
			System.out.println("s2 != s3");
		if(s1 == s3)
			System.out.println("s1 == s3");
		else
			System.out.println("s1 != s3");
		
		System.out.println();
		System.out.println();
		System.out.println();
		
		if(s1.equals(s2))//equals用于对象内容的比较
			System.out.println("s1 equals s2");
		else
			System.out.println("s1 not equals s2");
		if(s2.equals(s3))
			System.out.println("s2 equals s3");
		else
			System.out.println("s2 not equals s3");
		if(s1.equals(s3))
			System.out.println("s1 equals s3");
		else
			System.out.println("s1 not equals s3");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值