java数组

Java两大类数据类型:1.原始/基本/简单:8种

                                    2.引用/复合类型:数组(int,String)

区别:内存存贮结构1——栈   2——堆,栈

1.奇数数组

class aa {
	public static void main(String args[]) {
		/*
		 * int j=0; int[] v=new int[5000]; for(int i=1;i<10000;i++){ if(i%2==1)
		 * {v[j]=i;j++;}} for(j=0;j<v.length;j++) System.out.println(v[j]);
		 */

		int[] a = new int[50000];
		for (int i = 0; i < a.length; i++) {
			a[i] = i * 2 + 1;
			System.out.println(a[i]);
		}
	}
}


 

  1. 创建一个1-100之间的 素数数
           (只能被1和它本身整除的数字)

    class number4_2 {
    	public static void main(String args[]) {
    		int count = 0;
    		int[] v = new int[50];
    		for (int i = 2; i <= 100; i++) {
    			boolean x = true;
    			for (int j = 2; j < i / 2; j++) {//i/2减少循环次数
    				if (i % j == 0) {
    					x = false;
    					break;
    				}
    			}
    			if (x) {//放在循环外,全跑完才知道找没找到(上面的if语句从来没执行过)
    				v[count++] = i;
    			}
    		}
    		for (count = 0; count < v.length; count++)
    			System.out.println(v[count]);
    
    	}
    
    }


     

public class number4_3 {

	public static void main(String[] arg	int[] a={1,2,3,4,5};
		int[] b={4,5,6,7,8};
		int[] c=new int[a.length+b.length];
		int[] w=a;
		for(int i=0;i<c.length;i++){
			if(i==a.length)
  				w=b;
			c[i]=w[i%a.length];
			System.out.println(c[i]);
		}

	} 
}

i==a.length 只交换一次,i>=a.length  交换多次

i%a.length 利用循环

通过改变数组的栈来改变数组w,使其由数组a变为数组b,从而不使用if else;

3.a = {1,2,3,4,5}和b = {4,5,6,7,8}
        元素合并

class number4_3 {
	public static void main(String args[]) {
		int[] a = { 1, 2, 3, 4, 5 };
		int[] b = { 4, 5, 6, 7, 8 };
		int[] c = new int[10];
		for (int i = 0; i < a.length + b.length; i++) {
			if (i < a.length)
				c[i] = a[i];
			if (i >= a.length)
				c[i] = b[i - a.length];
		}
		for (int i = 0; i < c.length; i++) {
			System.out.println(c[i]);
		}
	}
}



<span style="font-size:18px;">public class number4_4 {

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

}</span>
  • int[] x={1,2,3,4,5}
    栈(数组的内存地址)     |      堆(具体的元素)
  • 数组交换时可以只改变栈,不改变堆
  1. 数组元素交换

    class number4_4 {
    	public static void main(String args[]) {
    		int[] a = { 1, 2, 3, 4, 5 };
    		int[] b = { 4, 5, 6, 7, 8 };
    		int[] temp = new int[5];
    		for (int i = 0; i < a.length; i++) {
    			temp[i] = a[i];
    			a[i] = b[i];
    			b[i] = temp[i];
    		}
    		for (int i = 0; i < a.length + b.length; i++) {
    			if (i < a.length)
    				System.out.println(a[i]);
    			else
    				System.out.println(b[i - a.length]);
    		}
    		for (int i = 0; i < a.length; i++) {
    			temp[i] = a[i];
    			a[i] = b[b.length - i - 1];
    			b[b.length - i - 1] = temp[i];
    		}
    		for (int i = 0; i < a.length + b.length; i++) {
    			if (i < a.length)
    				System.out.println(a[i]);
    			else
    				System.out.println(b[i - a.length]);
    		}
    
    	}
    }


  2. 找数
    class number4_5{
    	public static void main(String args[]){
    		int[] a={3,4,5,6,7,8};
    		int w=9;boolean x=true;
    	for(int i=0;i<a.length;i++){
    		if(a[i]==w){
    			x=false;
    			System.out.println(w+"在数组中");break;}
    			}
    	if(x) System.out.println(w+"不在数组中");
    		
    	
    		
    }	
    }
    
    


     

     

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值