Package java06; / * * 6 1--33 randomly generated number, and stores the list, and then traversing * * / Import of java.util.ArrayList; Import java.util.Random; public class Demolianxi { public static void {main (String [] args) the Random R & lt = new new the Random (); the ArrayList List = <Integer> new new the ArrayList <> (); for ( int I = 0; I <. 6; I ++ ) { int NUM = r.nextInt ( 33 is) + 1'd ; List.add (NUM); } System.out.println(list); System.out.println("========"); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)+ " "); } } }
Package Penalty for java06; Import java.util.ArrayList; / * Title: a custom class, add to the collection, and iterate ideas: 1, the definition of a student from class, four parts 2, create a collection, storage hard student objects, pan type <student> . 3, according to the class, to create four students objects 4, adds four students object to the collection the Add . 5, through the collection: for size GET * * / public class DemoArrayListStudent { public static void main (String [] args ) { the ArrayList <Student> List = new new the ArrayList <> (); Student One = new new Student ( "melon", 21 is ); Student TWO = new new Student ( "watermelon", 22); Student three = new Student("南瓜",23); Student four = new Student("杯瓜",24); list.add(one); list.add(three); list.add(two); list.add(four); System.out.println(list);//[java06.Student@282ba1e, java06.Student@13b6d03, java06.Student@f5f2bb7, java06.Student@73035e27] System.out.println("===================="); for (int i = 0; i < list.size(); i++) { Student stu = list.get(i); System.out.println("Name:" + stu.getName () + " , the age" + stu.getAge ()); / * * Name: melon, age 21 Name: pumpkin, age 23 Name: watermelon, age 22 Name: cup melon, age 24 * / } } } // Create student class Package java06; public class student { Private String name; Private int Age; public student () { } public student (String name, int Age) { the this .name = name; the this . = Age Age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Package java06; / * Title: define a set format to specify a method of printing (the ArrayList type as a parameter), except that {} from a set of spreading, using @ dividing each element System.out.pritln (list) [10,20,30] printArrayList (List) @ 20 is 10 {30} @ * * / Import of java.util.ArrayList; public class DemoArrayListPrint { public static void main (String [] args) { the ArrayList <Integer> = List new new the ArrayList <> (); List .add ( 10 ); List.add ( 20 is ); List.add ( 30 ); System.out.println (List); printArrayList (List); } / * Method three elements: 1. Return Value Type: This example knowledge printing, no operation, no results, so use void 2, Method name: printArratList . 3, the parameter list List * * / public static void printArrayList (the ArrayList List) { of System.out.print ( "{" ); for ( int I = 0; I <list.size (); I ++ ) { of System.out.print (List.get (I)); IF (I list.size == () -. 1 ) { System.out.println ( "}" ); } the else { of System.out.print ( "@" ); } } } }
Package java06; / * Title: with a large collection of random numbers is stored in 20, wherein the screening elements of even-numbered years and then, into which a small set of requirements: the method implemented by custom * * / Import of java.util.ArrayList; Import java.util.Random; public class DemoArrayListReturn { public static void main (String [] args) { the Random R & lt = new new the Random (); the ArrayList <Integer> = List new new the ArrayList <> (); for ( int I = 0; I <20 is; I ++ ) { int NUM = r.nextInt (100) + 1'd ; list.add(num); } ArrayList<Integer> smallList = douNumList(list); System.out.println(smallList); for (int i = 0; i <smallList.size(); i++) { System.out.println(smallList.get(i)); } } public static ArrayList<Integer> douNumList(ArrayList<Integer> list){ ArrayList<Integer> list2 = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { int num = list.get(i); if (num%2==0){ list2.add(num); } } return list2; } }