本文主要是介绍2020-11-14 java------------嵌套遍历集合,练习scanner和stringbuiled排序等API,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
嵌套集合遍历
import java.util.ArrayList;public class demo {public static void main(String[] args) {ArrayList<ArrayList<Student>> bigArraylist=new ArrayList<ArrayList<Student>>(); //创建大集合ArrayList<Student> firstArraylist=new ArrayList<>();Student s1=new Student("孙悟空",11);Student s2=new Student("猪八戒",12);Student s3=new Student("沙和尚",13);firstArraylist.add(s1);firstArraylist.add(s2);firstArraylist.add(s3);bigArraylist.add(firstArraylist);ArrayList<Student> secondAarrylist=new ArrayList<>();Student s11=new Student("诸葛亮",111);Student s22=new Student("刘备",122);Student s33=new Student("曹操",133);secondAarrylist.add(s11);secondAarrylist.add(s22);secondAarrylist.add(s33);bigArraylist.add(secondAarrylist);ArrayList<Student> lastAarrylist=new ArrayList<>();Student s111=new Student("杨幂",15);Student s222=new Student("唐嫣",16);Student s333=new Student("刘诗诗",17);secondAarrylist.add(s111);secondAarrylist.add(s222);secondAarrylist.add(s333);bigArraylist.add(lastAarrylist);//遍历集合for(ArrayList<Student> bigClass: bigArraylist)for (Student smallClass : bigClass)System.out.println(smallClass.getName()+"--------"+smallClass.getAge());}}
产生10个20以内不重复的随机数
import java.util.ArrayList;
import java.util.Random;public class demo {public static void main(String[] args) {Random random=new Random();ArrayList<Integer> array=new ArrayList<>();int count=0;while (count<10){int number=random.nextInt(20)+1; //0~19if (!array.contains(number)) {array.add(number);count++;}}for (Integer i : array)System.out.println(i);}}
键盘录入多个数据,以0结束,要求在控制台输出这些值中的最大值
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;public class demo {public static void main(String[] args) {Scanner sc=new Scanner(System.in);ArrayList<Integer> arry=new ArrayList<>();while (true){System.out.println("请输入数据:");int number=sc.nextInt();if (number!=0){arry.add(number);}elsebreak;}//toArray方法要求参数是数组//public <T> T[] toArray(T[] a)Integer[] i=new Integer[arry.size()];arry.toArray(i);Arrays.sort(i); //Arrays工具类下sort方法System.out.println("数组最大值是"+i[i.length-1]);}}
改动一下最后能实现输出数组并打印最大值
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;public class demo {public static void main(String[] args) {Scanner sc=new Scanner(System.in);ArrayList<Integer> arry=new ArrayList<>();while (true){System.out.println("请输入数据:");int number=sc.nextInt();if (number!=0){arry.add(number);}elsebreak;}Integer[] i=new Integer[arry.size()];arry.toArray(i);Arrays.sort(i);System.out.println("数组是:"+arryToString(i)+"最大值是:"+i[i.length-1]);//System.out.println("数组是:"+Arrays.toString(i)+"最大值是:"+i[i.length-1]);也能正确输出}public static String arryToString(Integer[] i){StringBuilder sb=new StringBuilder();sb.append("[");for (int x=0;x<i.length;x++){if(x!=i.length-1)sb.append(i[x]+",");elsesb.append(i[x]);}sb.append("]");return sb.toString();}
}
写了一个tostring方法把数组转换成字符串
为啥用stringbuilder不用string,回顾了一下原来是string不能修改,想要定义一个能修改的字符串就定义成stringbuffer或者stringbuilder类型,但是单线程用stringbuilder比较好,速度比较快,它没有线程安全。
数组类型没有tostring方法吗,为啥非要写一个,而最后sb却有tostring方法
原来直接i.tostring输出的是地址值,但是可以用Arrays.toString也能正确输出。练一下反正也行吧哈哈哈哈大可不必。
这篇关于2020-11-14 java------------嵌套遍历集合,练习scanner和stringbuiled排序等API的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!