final int prime = 31;重写hashcode方法时,这个常量的值是可以为任意值吗?
老师如下代码:
public class RemoveIteratorDemo { public static void main(String[] args) { List arr = new ArrayList(); arr.add("e"); arr.add("f"); arr.add("g"); arr.add(2); System.out.println(arr);//删除前的集合元素 System.out.println("---------"); for(Iterator it = arr.iterator(); it.hasNext();) { if("f".equals(it.next())) { it.remove();//删除指定元素 } System.out.println(it.next());//历遍集合元素。 } System.out.println("---------"); System.out.println(arr);//打印删除元素之后的集合 } }
运行运行完后结果是:
老师帮忙分析一下呗。。。。
看了这些解析方法,我们到底为什么要得到这些子节点、属性这些东西?我们为什么要解析xml文档?
Goods类:
package com.bjsxt.product2; public class Goods { private String name;//产品名称 private String bread;//品牌 private boolean isFlag = true;//用于标识是否有商品,假使为true是代表有商品,false代表没商品 public Goods(String name, String bread) { super(); this.name = name; this.bread = bread; } public Goods() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBread() { return bread; } public void setBread(String bread) { this.bread = bread; } //编写一个赋值方法 同步监视器为Goods类的对象 public synchronized void set(String bread, String name){ if(isFlag){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } this.setName(name); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setBread(bread); System.out.println("生产者线程生产了"+this.getBread()+"-------"+this.getName()); this.notify(); isFlag=true; } //编写一个取值的方法 public synchronized void get(){ if(isFlag=false){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("---消费者线程取走了"+this.getBread()+"----"+this.getName()); this.notify(); isFlag=false; } }
测试类:
package com.bjsxt.product2; public class Test { public static void main(String[] args) { //创建共享资源对象 Goods g = new Goods(); //创建生产者线程 Producter p = new Producter(g); //创建消费者线程 Customer c = new Customer(g); new Thread(c).start(); new Thread(p).start(); } }
运行截图
我把线程通信加进去之后运行结果和老师的不一样
radom创建的int型随机数和(int)(Math.radom())是一样的把
老师想问一下定义一个数组的时候必须规定数组长度吗,那为什么在使用main方法时public static void main(String[]args){}; String 无需定义数组长度?还有想问一下以下两种定义方法有什么区别
比如说在服务端内,读和写两个操作在同一个线程,这里是串行的方式,导致无法同时操作,所以就需要将这两个操作变成并行,既可以读,又可以写;之前的线程冲突是将并行的两个操作改为串行,这块和线程冲突不一样,可以这样理解吗?
老师。这里的super(name)就是调用的父类构造方法对吧,那是不是说明Thread类中也有一个有参的构造方法
老师好,这里的this对象为什么不是threadlocalmap而是threadlocal。
这是啥问题eclipse里不能直接创建xmlschema呀
太乱了 不知道啥是啥 老师讲的太快了有的地方也不说一下原因
package com.itbaizhan; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; /** * 用于接收客户端消息的线程类 */ class ChatReceive extends Thread{ private Socket socket; public ChatReceive(Socket socket){ this.socket = socket; } @Override public void run() { this.receiveMsg(); } /** * 实现接收客户端消息 */ private void receiveMsg(){ try(BufferedReader br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()))){ while (true){ String msg = br.readLine(); synchronized ("abc"){ //把读取到的数据写入公共区 ChatRoomServer.buf = "["+this.socket.getInetAddress()+"]"+msg; //唤醒发送消息的线程 "abc".notifyAll(); } } }catch (Exception e){ e.printStackTrace(); } } } /** * 向客户端发送消息的线程类 */ class ChatSend extends Thread{ private Socket socket; public ChatSend(Socket socket){ this.socket = socket; } @Override public void run() { this.sendMsg(); } /** * 实现向客户端发送消息 */ private void sendMsg(){ try(PrintWriter pw = new PrintWriter(this.socket.getOutputStream())){ while (true){ synchronized ("abc"){ //让发送消息的线程处于等待状态 "abc".wait(); //将公共区域中数据发送给客户端 pw.println(ChatRoomServer.buf); pw.flush(); } } }catch (Exception e){ e.printStackTrace(); } } } public class ChatRoomServer { //定义公共数据区 public static String buf; public static void main(String[] args) { System.out.println("Chat Server Version V1.0"); System.out.println("Listen at 8888........"); try(ServerSocket serverSocket = new ServerSocket(8888)){ while (true){ Socket socket = serverSocket.accept(); System.out.println("连接到:"+socket.getInetAddress()); //将与客户端对应的socket对象床底到接收消息的线程中 new ChatReceive(socket).start(); //将与客户端对应的socket对象床底到发送消息的线程中 new ChatSend(socket).start(); } }catch (Exception e){ e.printStackTrace(); } } } //以下是客户端代码 package com.itbaizhan; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * 发送消息的线程 */ class Send1 extends Thread { private Socket socket; private Scanner scanner; public Send1(Socket socket, Scanner scanner) { this.socket = socket; this.scanner = scanner; } @Override public void run() { this.sendMsg(); } /** * 发送消息 */ private void sendMsg() { try (PrintWriter pw = new PrintWriter(this.socket.getOutputStream());) { while (true) { String str = scanner.nextLine(); pw.println(str); pw.flush(); } } catch (Exception e) { e.printStackTrace(); } } } /** * 接收消息的线程 * */ class Receive1 extends Thread{ private Socket socket; public Receive1(Socket socket){ this.socket = socket; } @Override public void run() { this.receiveMSg(); } /** * 接收消息 */ private void receiveMSg(){ //创建用于接收对方消息的流对象 try(BufferedReader br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()))){ while (true){ String msg = br.readLine(); System.out.println("他说:"+msg); } }catch (Exception e){ e.printStackTrace(); } } } public class GoodTCP { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; try{ //键盘输入对象 Scanner scanner = new Scanner(System.in); System.out.println("请输入:Server,<port>或者:<IP>,<port>"); String str = scanner.nextLine(); String[] arr = str.split(","); //启动服务端 if("server".equals(arr[0])){ //启动服务端 System.out.println("TCP Server Listen at "+arr[1]+"......"); serverSocket = new ServerSocket(Integer.parseInt(arr[1])); socket= serverSocket.accept(); }else { //启动客户端 socket = new Socket(arr[0],Integer.parseInt(arr[1])); System.out.println("连接成功"); } //启动发送消息的线程 new Send1(socket,scanner).start(); //启动接收消息的线程 new Receive1(socket).start(); }catch (Exception e){ e.printStackTrace(); }finally { if (serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
启动客户端时,为啥报了索引越界。
老师看下,为什么我这代码会报错:
package com.liyang; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileStringDeme { public static void main(String[] args) { FileInputStream fileInputStream=null; FileOutputStream fileOutputStream=null; try{ fileOutputStream=new FileOutputStream("c:/flie/123.jpg"); fileOutputStream=new FileOutputStream("c:/flie/234.jpg"); int temp=0; while ((temp=fileInputStream.read())!=-1){ fileOutputStream.write(temp); } fileOutputStream.flush();//将数据从内存中写入到磁盘中 }catch (Exception e){ e.printStackTrace(); }finally { try { if (fileOutputStream!=null){ fileOutputStream.close(); } if (fileOutputStream!=null){ fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } } 下面是输出。
"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.2\lib\idea_rt.jar=54637:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\java\worksprce\untitled\out\production\untitled com.liyang.FileStringDeme java.lang.NullPointerException at com.liyang.FileStringDeme.main(FileStringDeme.java:16) Process finished with exit code 0
老师,请问为什么会报错啊
package com.bjsxt; /* 基于单向链表 */ public class MySinglyLinkedList<E> implements MyList<E> { /* 定义单向链表中的节点对象 */ class Node<E> { private E item; //存储元素 private Node next; //存储下一个节点对象的地址 Node(E item, Node next) { this.item = item; this.next = next; } } private Node head; //存放链表中的头节点 private int size; //记录元素个数 //向链表中添加元素 @Override public void add(E element) { //创建节点 Node<E> node = new Node<>(element, null); //找到尾节点 Node tail = getTail(); if (tail == null) { this.head = node; } else { tail.next = node; } //记录元素个数 this.size++; } private Node getTail() { //头节点是否存在 if (this.head == null) { return null; } //查找尾节点 Node node = this.head; while (true) { if (node.next == null) break; node = node.next; } return node; } //根据元素位置获取元素 @Override public E get(int index) { //校验index的合法性 this.checkIndex(index); //根据位置获取指定节点 Node<E> node = this.getNode(index); //将该节点中的元素返回 return node.item; } /* 对index进行校验 */ private void checkIndex(int index) { if (!(index > 0 && index < size)) { throw new IndexOutOfBoundsException(); } } /* 根据位置获取节点 */ private Node getNode(int index) { Node<E> node = this.head; for (int i = 0; i < index;i++) { node = node.next; } return node; } //获取元素个数 @Override public int size() { return size; } //根据元素位置删除元素 @Override public E remove(int index) { //检验index合法性 this.checkIndex(index); //根据位置找到该节点对象 Node<E> node = this.getNode(index); //获取该节点对象中的元素 E item = node.item; //将还节点对象从单向链表中删除 //判断当前删除订的节点是否尾头节点 if (this.head == node) { this.head = node.next; } else { Node<E> temp = this.head; for (int i = 0; i < index - 1; i++) { temp = temp.next; } temp.next = node.next; } node.next = null; //记录元素个数 this.size--; //将该元素返回 return item; } public static void main(String[] args) { MySinglyLinkedList<String> my=new MySinglyLinkedList<>(); my.add("a"); my.add("b"); my.add("c"); my.add("d"); System.out.println(my.size()); System.out.println(my.get(3)); System.out.println(my.remove(2)); for(int i=0;i<my.size();i++){ System.out.println(my.get(i)); } } }
package com.bjsxt; /* 基于链表结构存取元素的方法API定义 */ public interface MyList<E> { void add(E element); E get(int index); int size(); E remove(int index); }
老师这个标题的通配符的"通"字是统治的"统"吗?
为什么线程的名字没有被修改?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637