-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1614011121 申逸凡 #13
Comments
第二次作业熟悉一种设计模式(建造者模式)
import java.util.*;
public class Builder
{
private BaseSort sort;
public void readInput(double[] a)
{
System.out.println("请输入" + a.length + "个数字");
Scanner input = new Scanner(System.in);
for(int i = 0; i < a.length; i++)
{
a[i] = input.nextDouble();
}
}
public void writeOutput(double[] a)
{
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
}
public void produceBubbleSort(double[] a)
{
this.sort = new BubbleSort();
this.doSort(a);
}
public void produceInsertionSort(double[] a)
{
this.sort = new InsertionSort();
this.doSort(a);
}
public void produceQuickSort(double[] a)
{
this.sort = new QuickSort();
this.doSort(a);
}
public void produceSelectionSort(double[] a)
{
this.sort = new SelectionSort();
this.doSort(a);
}
public void doSort(double[] a)
{
this.readInput(a);
sort.sort(a);
this.writeOutput(a);
}
}
import java.util.Scanner;
public class TestBuilder
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Builder build = new Builder();
double[] a1 = new double[10];
double[] a2 = new double[10];
double[] a3 = new double[10];
double[] a4 = new double[10];
build.produceBubbleSort(a1);
build.produceInsertionSort(a2);
build.produceQuickSort(a3);
build.produceSelectionSort(a4);
}
} 运行结果:请输入10个数字 9 8 7 3 2 1 4 5 6 0 为什么有了接口类还要使用抽象类
|
第三次作业()一、 对Map的四种遍历1.第一种遍历:利用Map.KeySet + foreach遍历value package homework3;
import java.util.*;
public class TestMapIterator
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Map<String, String> studentInformation = new HashMap<>();
studentInformation.put("1614011101", "小张");
studentInformation.put("1614011102", "小赵");
studentInformation.put("1614011103", "小可");
studentInformation.put("1614011104", "小红");
studentInformation.put("1614011105", "小军");
studentInformation.put("1614011106", "小李");
studentInformation.put("1614011107", "小刘");
studentInformation.put("1614011108", "小王");
System.out.println("第一种遍历:利用Map.KeySet遍历value");
//KeySet返回所有Key的值
for(String learningNumber: studentInformation.keySet())
System.out.println("value = " + studentInformation.get(learningNumber));
System.out.println();
System.out.println("第二种遍历:通过Map.entrySet使用iterator遍历Key和value");
Iterator<Map.Entry<String, String>> iterator = studentInformation.entrySet().iterator();
//.entrySet()返回的是键值对的一个集合
//建议一个迭代器对象,它迭代的元素Map中的键值对
while(iterator.hasNext())
{
Map.Entry<String, String> entry = iterator.next();
System.out.println("key = " + entry.getKey() + " value = " + entry.getValue());
}
System.out.println();
System.out.println("第三种遍历: 通过Map.entrySet + foreach循环");
for(Map.Entry<String, String> entry : studentInformation.entrySet())
System.out.println("key = " + entry.getKey() + " value = " + entry.getValue());
System.out.println();
System.out.println("通过Map.value()来遍历所有的value,但不能遍历key");
for(String aValue : studentInformation.values())
System.out.println("value = " + aValue);
}
} 运行结果第一种遍历:利用Map.KeySet遍历value 第二种遍历:通过Map.entrySet使用iterator遍历Key和value 第三种遍历: 通过Map.entrySet + foreach循环 通过Map.value()来遍历所有的value,但不能遍历key 二、对Stream的入门级学习
import java.util.*;
import java.util.stream.Stream;
public class TestStream
{
/**
* 原始版本的Iterator,用户只能一个一个的遍历元素并对他执行某些操作,而Stream,用户只需要
* 给出对其包含的元素执行什么操作
*/
public static void main(String[] args)
{
System.out.println("利用stream的建造集合与遍历");
Stream<Integer> integerStream = Stream.of(1,3,3,4,5,6,6,8,9);
//forEach()
integerStream.forEach(number -> System.out.print(number + " "));
System.out.println();
//filter()
System.out.println("stream中过滤出大于等于5的元素");
Stream<Integer> integerStream2 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream2.filter(number -> number >= 5)
.forEach(number -> System.out.print(number + " "));
System.out.println();
//distinct()
System.out.println("删除重复的元素");
Stream<Integer> integerStream3 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream3.distinct()
.forEach(number -> System.out.print(number + " "));
System.out.println();
//sorted()
System.out.println("进行降序排序");
Stream<Integer> integerStream4 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream4.sorted((num1, num2) -> num2 - num1)
.forEach(number -> System.out.print(number + " "));
System.out.println();
//map()
System.out.println("对所有元素进行+1的操作");
Stream<Integer> integerStream5 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream5.map(number-> number + 1)
.forEach(number -> System.out.print(number + " "));
System.out.println();
// flatMap()
System.out.println("将两个集合合并");
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(4);
list2.add(5);
list2.add(6);
Stream<ArrayList<Integer>> stream = Stream.of(list1,list2);
stream.flatMap(list -> list.stream())
.forEach(number -> System.out.print(number + " "));
System.out.println();
}
} 运行结果利用stream的建造集合与遍历 |
The text was updated successfully, but these errors were encountered: