Java基础(数组)数组缩减
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java基础(数组)数组缩减
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            public class ArrayTest4 {public static void main(String[] args) {int[] arr = new int[10];for (int i = 0; i < arr.length; i++) {arr[i] = (int) (Math.random() * 20);}//遍歷for (int tmp : arr) {System.out.print(tmp + " ");}System.out.println();//數組的縮減//1.創建新數組int [] newArr = new int [arr.length/2];//2.依次把老數組中的相應的數據復制到新數組中for (int i = 0; i < newArr.length; i++) {newArr[i] = arr[i];}//3.老引用指向新數組,老數組變垃圾arr = newArr;//遍歷for (int tmp : arr) {System.out.print(tmp + " ");}}
} 
public class StudentTest3 {public static void main(String[] args) {Student[] stuArr = new Student[15];String[] names1 = {"趙", "錢", "孫", "李", "周", "吳", "鄭", "王", "馮", "陳", "褚", "衛", "蔣", "沈", "韓", "楊", "朱", "秦", "尤", "許", "穆", "蕭", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "貝", "明", "臧", "計", "伏", "成", "戴", "談", "宋", "茅", "龐", "熊", "紀", "舒","毋丘", "賀蘭", "綦毋", "屋廬", "獨孤", "南郭", "北宮", "王孫"};String[] names2 = {"娟", "英", "皋華", "慧", "巧", "美", "靜", "晉娜", "翠", "淑", "漆紅", "雙惠", "竹雅", "珠", "芝", "玉", "萍", "娥", "玲", "摯芬", "芳", "娜", "彩", "云賓", "環", "文惠", "雅", "珠", "春"};for (int i = 0; i < stuArr.length; i++) {int id = i + 1;int index1 = (int) (Math.random() * names1.length);int index2 = (int) (Math.random() * names2.length);String name = names1[index1] + names2[index2];int grade = (int) (Math.random() * 6 + 1);double score = (int) (Math.random() * 101);stuArr[i] = new Student(id, name, grade, score);}//遍歷for (Student tmp :stuArr) {System.out.println(tmp.toString());}System.out.println("==========================================================================");//1.創建新數組Student[] newStuArr = new Student[stuArr.length/2];//2.依次把老數組值傳入新數組for (int i = 0; i < newStuArr.length ; i++) {newStuArr[i] = stuArr[i];}//3.老引用指向新數組stuArr = newStuArr;//遍歷for (Student tmp :stuArr) {System.out.println(tmp.toString());}}
} 
                            
                        
                        
                        總結
以上是生活随笔為你收集整理的Java基础(数组)数组缩减的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Protobuf协议格式详解
- 下一篇: 数组添加/扩容和数组缩减
