0729~0802

Map键值对

map<k,v>

Map中的集合不能包含重复的键,值可以重复;每个键只能对应一个值

map常用方法

map.put()//添加键和值

//        put 插入键值对
//        map中 值可以重复 但是键不可以重复 如果重复 后面会覆盖前面的

map.putAll()//添加整个键值对

map.get()//查询

boolean containsKey(Object key)//判断键值是否存在返回true false

boolean containsValue(Object value)//判断value是否存在返回true false

boolean isEmpty()//是否为空

public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        Map<String, String> map1 = new HashMap<>();
        map.put("a", "1");
        map.put("c", "2");
        map.put("d", "3");
        map.put("b", "4");
        System.out.println(map);
        map1.put("e","9");
        map1.putAll(map);
        System.out.println(map1);
        System.out.println("*********************");
        map.remove("a");
        map1.clear();
        System.out.println(map);
        System.out.println(map1);
        System.out.println(map.get("c"));
        System.out.println(map.containsKey("a"));//判断key是否存在
        System.out.println(map.containsValue("3"));//判断Value是否存在
        System.out.println(map1.isEmpty());//判断是否为空
    }

HashMap和Hashtable

public static void main(String[] args) {
        //HashMap
        //是线程不安全的,并允许使用 null 值和 null 键。
        Map<String,String> map=new HashMap<>();
        map.put("","1");
        map.put(null,"2");
        map.put("d",null);
        map.put("b","4");
        System.out.println(map);
        //Hashtable 无序的 (我们说的有序是根据输入的顺序)
        Map<String,String> map1=new Hashtable<>();
        map1.put("a","1");
        map1.put("c","2");
        map1.put("d","3");
        map1.put("b","4");
        System.out.println(map1);
    }

TreeMap和LinkedHashMap

public static void main(String[] args) {
        //TreeMap(按照自然顺序排列的)
        Map<String,String> map1=new TreeMap<>();
        map1.put("a","1");
        map1.put("c","2");
        map1.put("d","3");
        map1.put("b","4");
        System.out.println(map1);
        //LinkedHashMap 有序的
        Map<String,String>map=new LinkedHashMap<>();
        map.put("a","1");
        map.put("c","2");
        map.put("d","3");
        map.put("b","4");
        System.out.println(map);
    }

map的遍历

先要创建一个集合存放map键值对的key值 ,然后通过key  get()查询到value

  1. foreach循环
  2. iterator迭代器
public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("a", "1");
        map.put("d", "2");
        map.put("c", "3");
        map.put("b", "4");
        System.out.println(map.get("c"));
        System.out.println(map);

        Set<String> keys = map.keySet();
//        for (String key : keys) {
//            System.out.println(key + ":" + map.get(key));
//        }
        Iterator<String> ite= keys.iterator();
        while (ite.hasNext()){
           String key=ite.next();
            System.out.println(key+":"+map.get(key));
        }

    }

File类

//        文件IO : 文件读取 input输入    文件写入 output输出
//        file类  InputStream输入流     OutputStream输出流
public static void main(String[] args) {
        String path ="D:\\IntelliJ IDEA 2021.3.1\\Project\\0729\\day01-1";
        String path1="D:/IntelliJ IDEA 2021.3.1/Project/0729/day01-1";
        String path2 ="/user/local/nginx";
        System.out.println(path);
        System.out.println(path1);
        System.out.println(path2);
    }

 方法

public String getName() :返回由此File表示的文件或目录的名称。

public long length() :返回由此File表示的文件的长度。

public String getPath() :将此File转换为路径名字符串。

public boolean exists()` :此File表示的文件或目录是否实际存在。

public boolean isDirectory()` :此File表示的是否为目录。

public boolean isFile()` :此File表示的是否为文件。

public static void main(String[] args) {
        File file =new File("D:\\abc\\1.txt");
        System.out.println(file);
        System.out.println(file.exists());//文件是否存在
        System.out.println(file.isDirectory());//是否是文件夹
        System.out.println(file.isFile());//是否是文件
        System.out.println(file.length());//文件长度 大小 字节
        System.out.println(file.getPath());//获取当前文件路径
    }

 Flie的遍历

file.list()

file.listFiles()

public static void main(String[] args) {
        String path = "D:\\IntelliJ IDEA 2021.3.1\\Project\\0729\\day01-1";
        File file = new File(path);

//        String[] str = file.list();
//        for (String f : str) {
//            System.out.println(f);
//        }

        File[] files = file.listFiles();
        for (File ff : files) {
            System.out.println(ff.getName());//当前文件名称
            System.out.println(ff);//路径
        }
    }

 创建与删除

 public static void main(String[] args) {
//        mkdir  创建的是文件夹 目录  如果父级目录不存在 无法创建
//        mkdirs  创建的是文件夹 目录  如果父级目录不存在 也可以创建
        File file=new File("D:\\game\\aa\\bbb\\ccc");
        //mkdir 创建的是文件夹 不能创建多级目录
        //file.mkdir();
        //mkdirs可以创建多级目录
        file.mkdirs();
        System.out.println(file);
        System.out.println(file.isFile());
        System.out.println(file.exists());
        //delete 只能删除当前的空的文件夹 只能删除一个
        System.out.println(file.delete());
        System.out.println(file);
    }

IO流

  • 输入流 :把数据从其他设备上读取到内存中的流。

    • 以InputStream,Reader结尾

  • 输出流 :把数据从内存 中写出到其他设备上的流。

    • 以OutputStream、Writer结尾

  • 字节流 :以字节为单位,读写数据的流。

    • 以InputStream和OutputStream结尾

  • 字符流 :以字符为单位,读写数据的流。

    • 以Reader和Writer结尾

字节输入流

public static void main(String[] args) throws Exception {
        InputStream is =new FileInputStream("src/Java001.txt");
        File file=new File("src/Java001.txt");
        byte[] bytes=new byte[(int) file.length()];

        int num;
        while ((num= is.read(bytes))!=-1){
            System.out.println(new String(bytes,0,num));
        }
        is.close();
    }

 字节的输入定义一个byte数组,把读到的字节保存到数组中,通过循环遍历读出保存的 ,new String 把保存的转换成字符串从0开始到num(读多少个字节)

字节输出流

  public static void main(String[] args) throws IOException {
        OutputStream os =new FileOutputStream("src/Java001.txt");
        os.write('s');
        os.write('v');
        os.write('a');
        os.write('\n');
        byte[] bytes="今天天气不太好啊!!!".getBytes();
        os.write(bytes);
        os.close();
    }

字符输入流

public static void main(String[] args) throws IOException {
        Reader reader = new FileReader("src/Java002.txt");

        char[] chars=new char[1];
        int num;
        while ((num=reader.read(chars))!=-1){
            System.out.print(new String(chars,0,num));
        }
        reader.close();
    }

字符输出流

public static void main(String[] args) throws IOException {
        Writer wi =new FileWriter("src/Java002.txt");

        wi.write("今天有太阳");
        wi.write("\n");
        wi.write("abc");

        wi.close();
    }

复制文件

 public static void main(String[] args) throws IOException {
        InputStream is =new FileInputStream("src/Java001.txt");
        File file =new File("src/Java001.txt");
        
        OutputStream os =new FileOutputStream("001.txt");

        byte[] bytes=new byte[(int) file.length()];
        while (is.read(bytes)!=-1){
            os.write(bytes);
        }
        is.close();
        os.close();
    }

以上的写入,如果从新运行并写入之前的东西会被覆盖,如果想要继续在后面添加在文件后方加true表示允许继续追加,false表示不允许追加,可省略不写

public static void main(String[] args) throws IOException {
        Writer wi =new FileWriter("src/Java002.txt",true);

        wi.write("今天有太阳");
        wi.write("\n");
        wi.write("abc");

        wi.close();
    }

缓存流

字节流

 InputStream is = new FileInputStream("E:\\abcd\\img.jpg");
        InputStream bis = new BufferedInputStream(is);
        OutputStream os = new FileOutputStream("D:\\qqqwww\\img1.jpg");
        OutputStream bos = new BufferedOutputStream(os);

 字符流

                    Writer w = new FileWriter("src/java01.txt", true);
                    BufferedWriter bw = new BufferedWriter(w);

                    Reader r = new FileReader("src/java02.txt");
                    BufferedReader br = new BufferedReader(r);

转换流

                InputStream r = new FileInputStream("src/java02.txt");
                Reader r2 = new InputStreamReader(r,"GBK");
                BufferedReader br = new BufferedReader(r2);
 Writer w = new OutputStreamWriter(new FileOutputStream("src/java03.txt", true),"GBK");
             BufferedWriter bw = new BufferedWriter(w);

数据流

                OutputStream os =new FileOutputStream("src/java03.txt");
                DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(new FileInputStream("src/java03.txt"));
dos.writeInt(150);
dos.writeDouble(3.1415926);
dos.writeBoolean(true);
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());

输出必须按照输入的顺序写

对象流

                 OutputStream os =new FileOutputStream("src/java04.txt");
                ObjectOutputStream oos = ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/java04.txt")); new ObjectOutputStream(os);

创建对象 ,给对象赋值 把对象写入文件里

序列化

向文件中保存一个对象的过程就叫做 序列化

该类必须实现java.io.Serializable 接口

JDK8的新特性

lambda表达式

//        箭头函数  lambda表达式
//        (参数)->{ 代码体 lambda表达式 }
public static void main(String[] args) {

//        面向对象编程
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        }).start();

        //  函数编程思想
        new Thread(() -> {
            System.out.println("hello world 世界");
        }).start();

    }

java.util.function包四大类函数式接口

消费型接口        有参无返回

供给型接口        无参有返回

Supplier<String> supplier =
                ()->new String[]{"石头", "剪刀", "布"}[(int) (Math.random() * 3)];
        System.out.println(supplier.get());

判断型接口        有参返回值是布尔型

 Predicate<String> predicate = (sb)->{
            for (int i = 0; i < sb.length(); i++) {
                char c = sb.charAt(i);
                if(!((c >= 'a' && c <= 'z' )  ||
                        (c>='A' && c<='Z') ||
                        (c>='0' && c<='9'))){
                    return false;
                }
            }
            return true;
        };
        System.out.println(predicate.test("ainiainiaini"));

功能型接口        有参有返回

IntFunction<String> iF = (cont)->{
            int [] arr = new int[cont];
            Arrays.fill(arr,cont);
            return Arrays.toString(arr);
        };
        System.out.println(iF.apply(1));
        System.out.println(iF.apply(2));

自定义型接口

方法引用

Lambda表达式是可以简化函数式接口的变量与形参赋值的语法。而方法引用和构造器引用是为了简化Lambda表达式的。

DoubleUnaryOperator duo = Math::floor;
        System.out.println(duo.applyAsDouble(3.5));
        System.out.println(duo.applyAsDouble(9.99));
IntBinaryOperator ibo = Math::max;
        System.out.println(ibo.applyAsInt(1, 2));
        System.out.println(ibo.applyAsInt(150, 200));

StreamAPI

1- 创建 Stream:通过一个数据源(如:集合、数组),获取一个流

2- 中间操作:每次处理都会返回一个持有结果的新Stream,即中间操作的方法返回值仍然是Stream类型的对象,因此中间操作可以是个操作链,可对数据源的数据进行n次处理,但是在终结操作前,并不会真正执行。

3- 终止操作:终止操作的方法返回值类型就不再是Stream了,因此一旦执行终止操作,就结束整个Stream操作了。一旦执行终止操作,就执行中间操作链,最终产生结果并结束Stream。

public static void main(String[] args) {
//        不会对源数据造成改变
//        1. 创建
        Stream<Integer> stream = Stream.of(55, 12, 7,54,21);
//        System.out.println(stream);

//        2.中间操作
        stream = stream.sorted();

//        3.终止操作
        stream.forEach(System.out::println);
    }
public static void main(String[] args) {
        int[] arr = {1, 3, 46, 46, 46, 46, 46, 73, 23, 46, 74, 34, 56, 3, 456};
        Arrays.stream(arr)
                .sorted()//排序
                .filter((num) -> num < 100)// filter 方法用于通过设置的条件过滤出元素
                .distinct()//去重复值
                .skip(2)//跳过前两个
                .forEach(System.out::println);
    }

  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目是一个典型的BP神经网络的应用题目,需要我们用Matlab编程实现。下面是具体的算法步骤: 1.加载数据集 P 和 T。 2.初始化神经网络参数,包括输入层、输出层和隐层的神经元个数等。 3.将数据集进行划分,一部分用于训练神经网络,一部分用于测试神经网络的性能。 4.定义神经网络的激活函数,一般选择 sigmoid 函数。 5.进行神经网络的训练,采用反向传播算法进行权值的更新。 6.在训练过程中,根据测试集的误差,选择合适的隐层神经元个数。 7.利用训练好的神经网络对测试集进行预测,计算误差。 8.根据误差确定选择哪种隐单元个数的网络可得到相对最好的BP网络。 下面是Matlab代码的实现: ```matlab % 加载数据集 P = [-1:0.1:1]; T = [-0.96,-0.577,-0.0729,0.377,0.641,0.66,0.461,0.1336,-0.201,-0.434,-0.5,-0.393,-0.1647,0.0988,0.3072,0.396,0.3449,0.1816,-0.0312,-0.2183,-0.3201]; % 初始化神经网络参数 input_layer_size = 1; hidden_layer_sizes = [6 7 8 9 10 11 12]; output_layer_size = 1; max_iterations = 1000; learning_rate = 0.1; % 划分数据集 train_ratio = 0.7; train_size = floor(length(P) * train_ratio); train_P = P(1:train_size); train_T = T(1:train_size); test_P = P(train_size+1:end); test_T = T(train_size+1:end); % 定义激活函数 sigmoid = @(x) 1./(1+exp(-x)); % 训练神经网络 for i = 1:length(hidden_layer_sizes) % 初始化权值 W1 = randn(hidden_layer_sizes(i), input_layer_size); W2 = randn(output_layer_size, hidden_layer_sizes(i)); % 训练神经网络 for j = 1:max_iterations % 前向传播 a1 = train_P; z2 = W1 * a1; a2 = sigmoid(z2); z3 = W2 * a2; a3 = sigmoid(z3); % 反向传播 delta3 = (a3 - train_T) .* a3 .* (1 - a3); delta2 = W2' * delta3 .* a2 .* (1 - a2); % 更新权值 W2 = W2 - learning_rate * delta3 * a2'; W1 = W1 - learning_rate * delta2 * a1'; end % 利用训练好的神经网络预测测试集 a1 = test_P; z2 = W1 * a1; a2 = sigmoid(z2); z3 = W2 * a2; a3 = sigmoid(z3); % 计算误差 error(i) = mean(abs(a3 - test_T)); end % 确定选择哪种隐单元个数的网络可得到相对最好的BP网络 [~, idx] = min(error); optimal_hidden_layer_size = hidden_layer_sizes(idx); fprintf('相对最好的BP网络的隐单元个数为:%d\n', optimal_hidden_layer_size); ``` 以上就是利用Matlab实现BP神经网络的算法步骤和代码实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值