• java新手笔记4 数组


    1.数组

    import java.util.Random;
    public class ArrayDemo1 {
    
    	public static void main(String[] args) {
    		int a = 10;
    		int b = 20;//
    		//数组对象  创建10个变量  arr[0]、arr[1]、arr[2]、arr[3]...、arr[9]
    		int[] arr = new int[10];//默认0
    		arr[0] = 35;
    		arr[1] = 29;
    		System.out.println("arr[0] = " + arr[0]);
    		System.out.println("arr[1] = " + arr[1]);
    		//利用for循环访问数组
    		for(int i = 0; i < 10; i++ ) {
    			System.out.print("arr[1] = " + arr[i] + "	");
    		}
    		System.out.println("===================");
    		Random ran = new Random();
    		for(int i = 0; i < 10; i++ ){
                arr[i] = ran.nextInt(101);
    			System.out.println("arr["+i+"] = " + arr[i]);
    		}
    
    		//arr[10] = 100;//ArrayIndexOutOfBoundsException: 10
    		//arr[9] = 3.14;类型匹配
    
    
    		Random[] r = new Random[3];
    		r[1] = ran;
    
    
    	}
    }
    

     2.改变长度

    import java.util.Random;
    public class ArrayDemo2 {
    
    	public static void main(String[] args) {
    		Random ran = new Random();
    		int[] a = new int[8];
    		
    		for(int i = 0; i < a.length; i++ ) {
    			a[i] = ran.nextInt(41) + 60;
    		}
    		
    		for(int i = 0; i < a.length; i++ ){            
    			System.out.println("a["+i+"] = " + a[i]);
    		}
    		System.out.println("--------------------");
    		a = new int[5];//改变长度 length属性
    
    		for(int i = 0; i < a.length; i++ ){            
    			System.out.println("a["+i+"] = " + a[i]);
    		}
    
    
    	}
    }
    

     3.初始化

    import java.util.Random;
    public class ArrayDemo3 {
    
    	public static void main(String[] args) {
    		Random ran = new Random();
    		//double[] a = new double[]{3.14, 98, 5.23, 6.14, 100};//数组初始化
    		//char[] a = {'中','国','影','分','身','B','w'};//创建对象
    		
    		/*
    		String s = new String("yema");
    		String[] a = null;//对象可以null
    		//int b = null;
    		a = new String[8];
    		a[3] = s;
    		*/
    		String[] a = {"javase","oracle","java web"};
    
    		for(int i = 0; i < a.length; i++ ){            
    			System.out.println("a["+i+"] = " + a[i]);
    		}
    		
    
    	}
    }
    

     4.数组遍历

    import java.util.Random;
    public class ArrayDemo4 {
    
    	public static void main(String[] args) {
    		Random ran = new Random();
    		int[][] a = new int[3][4];//3行 4列
            //System.out.println("a.length = " + a.length);
    		//System.out.println("a[1].length = " + a[1].length);
    
    		//赋值
    		for(int i = 0; i < a.length; i++ ){            
    			for(int j = 0; j < a[i].length; j++) {
                   a[i][j] = ran.nextInt(101);
    			}
    		}
    		//输出
    		for(int i = 0; i < a.length; i++ ){ 
    			for(int j = 0; j < a[i].length; j++) {
    				//System.out.print("a["+i+"]["+j+"] = " + a[i][j] + "  ");
    				System.out.print( a[i][j] + "  ");
    			}
    			System.out.println();
    		}
    		
    
    	}
    }
    

     5.二维数组

    import java.util.Random;
    public class ArrayDemo5 {
    
    	public static void main(String[] args) {
    		Random ran = new Random();
    		int[][] a = new int[3][];//3行 
    
    		a[0] = new int[]{18};
    		int[] k = {20,50,90};
    		a[1] = k;
    		a[2] = new int[]{100,300};
            
    		//输出
    		for(int i = 0; i < a.length; i++ ){ 
    			for(int j = 0; j < a[i].length; j++) {
    				
    				System.out.print( a[i][j] + "  ");
    			}
    			System.out.println();
    		}
    		
    		String[][] s = { //赋初值
    			              {"javaEE","hibernate","spring"},
    			              {"struts","jquery"}
    						};
    
            s[1][1] = "yema";
            //输出
    		for(int i = 0; i < s.length; i++ ){ 
    			for(int j = 0; j < s[i].length; j++) {
    				
    				System.out.print( s[i][j] + "  ");
    			}
    			System.out.println();
    		}
    	}
    }
    
  • 相关阅读:
    南京师范大学2021年高等代数数学分析考研试题及参考解答
    南昌大学2021年数学分析考研试题参考解答
    南昌大学2021年高等代数考研试题参考解答
    河南师范大学2021年数学分析考研试题参考解答
    河海大学2021年数学分析考研试题参考解答
    河海大学2021年高等代数考研试题参考解答
    合肥工业大学2021年高等代数数学分析考研试题参考解答
    福州大学2021年高等代数数学分析考研试题参考解答
    东华大学2021年高等代数数学分析考研试题参考解答
    安徽大学2021年数学分析考研试题参考解答
  • 原文地址:https://www.cnblogs.com/feilongblog/p/4656721.html
Copyright © 2020-2023  润新知