黑马程序员技术交流社区
标题:
请问这两个增强for什么意思
[打印本页]
作者:
wu971856966
时间:
2016-11-11 21:57
标题:
请问这两个增强for什么意思
import java.util.ArrayList;
import com.heima.bean.Person;
public class Demo5_ArrayListArrayList {
/**
* * A:案例演示
* 集合嵌套之ArrayList嵌套ArrayList
* 案例:
* 我们学科,学科又分为若个班级
* 整个学科一个大集合
* 若干个班级分为每一个小集合
*/
public static void main(String[] args) {
ArrayList<ArrayList<Person>> list = new ArrayList<>();
ArrayList<Person> first = new ArrayList<>(); //创建第一个班级
first.add(new Person("杨幂", 30));
first.add(new Person("李冰冰", 33));
first.add(new Person("范冰冰", 20));
ArrayList<Person> second = new ArrayList<>();
second.add(new Person("黄晓明", 31));
second.add(new Person("赵薇", 33));
second.add(new Person("陈坤", 32));
//将班级添加到学科集合中
list.add(first);
list.add(second);
//遍历学科集合
for(ArrayList<Person> a : list) {
for(Person p : a) {
System.out.println(p);
}
}
}
}
作者:
wu971856966
时间:
2016-11-11 21:59
如题 好像删掉一个也可以
作者:
飞天神猫
时间:
2016-11-11 22:19
package com.heima.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {
/**
* 需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
*
* 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
* 分析:
* 1,在目标文件夹中创建原文件夹
* 2,获取原文件夹中所有的文件和文件夹,存储在File数组中
* 3,遍历数组
* 4,如果是文件就用io流读写
* 5,如果是文件夹就递归调用
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File src = Test1.getDir();
File dest = Test1.getDir();
if(src.equals(dest)) {
System.out.println("目标文件夹是源文件夹的子文件夹");
}else {
copy(src,dest);
}
}
/*
* 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
* 1,返回值类型void
* 2,参数列表File src,File dest
*/
public static void copy(File src, File dest) throws IOException {
//1,在目标文件夹中创建原文件夹
File newDir = new File(dest, src.getName());
newDir.mkdir();
//2,获取原文件夹中所有的文件和文件夹,存储在File数组中
File[] subFiles = src.listFiles();
//3,遍历数组
for (File subFile : subFiles) {
//4,如果是文件就用io流读写
if(subFile.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
//5,如果是文件夹就递归调用
}else {
copy(subFile,newDir);
}
}
}
}
作者:
Jarc
时间:
2016-11-12 00:32
多看api
作者:
Autumn、Yan
时间:
2016-11-12 10:24
第一个for是遍历两个班级,第二个是遍历班级里的学生,这样就把所有学生都遍历出来了
作者:
L10052108
时间:
2016-11-12 11:13
集合嵌套使用!
作者:
wu971856966
时间:
2016-11-12 17:35
详细一点呢
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2