(1)把数组中大于10的数字放到一个list集合中,并打印到控制台。
public static void main(String[] args) {
double[] doubleArr = new double[10];
List<Double> list = new ArrayList<>();
storeRandomNumber(doubleArr);
storeInList(doubleArr,list);
System.out.println(list);
System.out.println("==============");
getListElement(list);
}
private static void storeInList(double[] doubleArr, List<Double> list) {
for(double d:doubleArr){
if(d>10){
list.add(d);
}
}
}
private static void storeRandomNumber(double[] doubleArr) {
for(int i = 0;i<10;i++){
Random random = new Random();
doubleArr[i] = 1+random.nextDouble()*100;
}
}
(2)把数组中的数字放到当前文件夹的number.txt文件中
private static void getListElement(List<Double> list) {
for(int i=0;i<list.size();i++){
double d =list.get(i);
printInNumber(d);
}
}
private static void printInNumber(double d) {
String str = d+"";
FileOutputStream fos = null;
try {
fos = new FileOutputStream("E:\\abc.txt",true);
fos.write(str.getBytes());
fos.write('-');
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
throw new RuntimeException("写入失败!");
} finally{
try{
if(fos!=null)
fos.close();
} catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("释放资源失败!");
}
}
}