package com.bjsxt.test3; /** * 基于双向链表实现元素存取的容器 * @param <E> */ public class MyDoubleLinkedList<E> implements MyList<E>{ //定义双向链表的节点对象 class Node<E>{ E item;//记录元素 Node<E> prev;//记录前一个节点对象 Node<E> next;//记录后一个节点对象 public Node(E item, Node<E> prev, Node<E> next) { this.item = item; this.prev = prev; this.next = next; } } private Node head;//记录头节点 private Node tail;//记录尾节点 private int size;//记录元素个数 /** *向双向链表中添加元素 * @param element */ @Override public void add(E element) { this.LinkLast(element); } //将节点对象添加到双向链表的尾部 private void LinkLast(E element){ //获取尾节点 Node t=this.tail; Node<E> node=new Node<>(element,t,null); //将新节点定义为尾节点 this.tail=node; if(t==null){ this.head=node; }else{ this.tail=node; } this.size++; } /** * 根据指定位置获取元素 * @param index * @return */ @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<this.size)){ throw new IndexOutOfBoundsException("index:"+index+"size:"+this.size); } } //根据位置获取指定节点对象 private Node getNode(int index){ //判断当前位置距离头或者尾哪个节点更近 if(index < ( this.size >> 1 ) ){ Node node=this.head; for(int i=0;i<index;i++){ node=node.next; } return node; }else{ Node node=this.tail; for(int i=this.size-1;i>index;i--){ node=node.prev; } return node; } } /** * 返回元素的个数 * @return */ @Override public int size() { return this.size; } /** * 根据指定位置删除元素 * @param index * @return */ @Override public E remove(int index) { //对index进行合法性校验 this.checkIndex(index); //根据指定位置获取节点对象 Node<E> node=this.getNode(index); //获取节点对象中的元素 E item=node.item; //判断当前节点是否为头节点 if(node.prev==null){ this.head=node.next; }else{ //完成当前节点的直接前驱节点和当前节点的直接后继节点的挂接 node.prev.next=node.next; } //判断当前节点是否为尾节点 if(node.next==null){ this.tail=node.prev; }else{ //完成当前节点的直接后继节点和当前节点的直接前驱节点的挂接 node.next.prev=node.prev; } //当前节点断掉与它直接前驱节点的连接 node.prev=null; //当前节点断掉与它直接后继节点的连接 node.next=null; node.item=null; //记录元素个数 this.size--; return item; } public static void main(String[] args) { MyList<String> myList=new MyDoubleLinkedList<>(); myList.add("a"); myList.add("b"); myList.add("c"); myList.add("d"); System.out.println(myList.remove(0)); System.out.println(myList.size()); for(int i=0;i<myList.size();i++){ System.out.println(myList.get(i)); } } }
老师 我看了半天也没看出哪里错了 62行和144行都没有问题呀 为啥会报空指针异常
老师:
您好,对电话本序列化和反序列化的操作,当我输入一条电话信息时,能在我的电脑上有一个电话本的文件,并且能读出来,你看我的代码是这样写的:
ObjectOutputStreamDemo { (String[] args) { ObjectOutputStream oos = { oos = ObjectOutputStream(FileOutputStream())Person person = Person()oos.writeObject(person)oos.flush()}(Exception e) { e.printStackTrace()}{ { (oos != ) { oos.close()} }(Exception e) { e.printStackTrace()} } } }
public class ObjectInputStreamDemo { public static void main(String[] args) { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("e:/电话本.txt")); Person person = (Person) ois.readObject(); System.out.println("序号:"+person.getId()+"\t"+ "姓名:"+person.getName()+"\t"+ "年龄:"+person.getAge()+"\t"+ "性别:"+person.getSex()+"\t"+ "电话号码:"+person.getTelNum()+"\t"+ "地址:"+person.getAddress()+"\t"); }catch (Exception e) { e.printStackTrace(); }finally { try { if(ois != null) { ois.close(); } }catch (Exception e) { e.printStackTrace(); } } } }
老师,这里取馒头里面的notify为什么是唤醒放馒头的线程呢;这里的index--不应该放在最后吗,现在不是提前减了吗;这里面为什么都要加this啊
ServerSocket对象创建后不用关闭吗,为什么只关闭Socket对象
package com.bjsxt.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** * 发送信息线程 */ class ClientSend extends Thread{ private Socket socket; ClientSend(Socket socket){ this.socket = socket; } @Override public void run() { this.sendMsg(); } /** * 发送消息 */ private void sendMsg(){ Scanner scanner = null; PrintWriter pw = null; try { scanner = new Scanner(System.in); pw = new PrintWriter(this.socket.getOutputStream()); String str = scanner.nextLine(); while (true){ if ("exit".equals(str)){ break; } pw.println(str); pw.flush(); } }catch (Exception e){ e.printStackTrace(); }finally { if (pw!=null){ pw.close(); } if (scanner!=null){ scanner.close(); } } } } /** * 接受消息线程 */ class ClientReceive extends Thread{ private Socket socket; ClientReceive(Socket socket){ this.socket = socket; } @Override public void run() { this.receiveMsg(); } /** * 接受消息 */ private void receiveMsg(){ BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); while (true){ String clientStr = br.readLine(); if ("exit".equals(clientStr)){ break; } System.out.println("客户端说:"+clientStr); } }catch (Exception e){ e.printStackTrace(); }finally { if (br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } public class ChatSocketClient { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1",8888); System.out.println("连接成功!"); new ClientReceive(socket).start(); new ClientSend(socket).start(); } catch (IOException e) { e.printStackTrace(); } } }
老师,为啥我这里死循环了,大家都用的where(true)了呀,而且我前面的双向通信和单向都用的了where(true)
和老师写的差不多,到点对点练习就死循环了
在这个图里面,"hello"这个链表里面,他的上一个链表是"java"还是first的地址?
/** * 创建网络编程中的服务端 */ public class BasicSocketServer { public static void main(String[] args) { Socket socket=null; BufferedReader br=null; try { ServerSocket serverSocket=new ServerSocket(8888);//此处ServerSocket构造方法中的参数是int类型的端口号 System.out.println("服务器启动监听,等待监听。。。。。。"); //开启端口的监听, // 此方法返回的是Socket对象,当前那个客户端来连接就是针对哪个客户端的Socket对象 socket= serverSocket.accept();//可以用这个Socket对象来读取客户端发送过来的对象 //读取客户端发送的消息,从Socket中获取的信息都是字节信息 br=new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(br.readLine()); } catch (IOException e) { e.printStackTrace(); }finally { try { if(br!=null){ br.close(); } if(socket !=null){ socket.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
public class BasicSocketClient { public static void main(String[] args) { Socket socket=null; PrintWriter pw=null; try { //创建socket 对象,在狗杂方法中需要创建两个对象:服务端的IP地址,及服务端的监听端口 socket=new Socket(InetAddress.getLocalHost().getHostAddress(),8888); // socket=new Socket("127.0.0.1",8888); pw=new PrintWriter(socket.getOutputStream()); pw.println("服务端你好"); pw.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if(pw !=null){ pw.close(); } if(socket !=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
显示:
问题: 访问控制台没有东西输出
在下面的问答看到有同学问jaxen的jar导它干什么,我在idea中,只导入了dom4j的jar,美元导入jaxen的jar。
在执行到第17行的xpath表达式的时候,报了异常,找不到类异常org.jaxen.JaxenException
导入jaxen的包后再次运行,成功
所以jaxen的jar应该主要是用来帮助解析dom4j提供的selectNodes()或selectSingleNode()等等方法中使用的xpath表达式的。少了它会报异常。
List<Node> list = doc.selectNodes("//author"); for (Node n:list){ System.out.println("Node name is :" +n.getName()+"\t"+n.getText()); } //choose id List<Node> attlist = doc.selectNodes("//book/@id"); for (Node att:attlist){ System.out.println("Node name is :" +att.getName()+"\t"+att.getText()); } }
现在必须要用<Node>类型不报错,跟视频冲突,请您去查文档。并且更新教程
如果我有错请您找出来
老师,这怎么报错了?tnrow一般是写在方法那里把异常抛出去是吗?
老师我想请问一下
按照课堂的code
地址的字母之间 不能存在空格
比如
beijing china
请问一下怎么解决
老师讲的是不是有点问题,我看了一下构造函数
public PrintWriter (Writer out) { this(out, false); } public PrintWriter(Writer out, boolean autoFlush) { super(out); this.out = out; this.autoFlush = autoFlush; }
不指定自动刷新时,PrintWriter是false,不会自动刷新的,但老师直接用的第一个构造函数,然后就说不需要flush了(课件上也这样写)。结果不出错是因为close时刷新了缓冲区。
既然Integer可以直接进行比较,compareto的意义是什么
public class MyThread4 implements Runnable{ @Override public void run() { for(int i=0;i<10;i++) { System.out.println("i="+i); } } } public class TestStop { public static void main(String[] args) { MyThread4 my=new MyThread4(); Thread t=new Thread(my); t.start();//启动循环 //主线程中的循环 for(int i=0;i<10;i++) { if(i==3) { t.stop();//已过时,不建议使用 } System.out.println(Thread.currentThread().getName()+"-----"+i); } } }
运行结果:
问题:为什么有的i=0与main-----3在同一行?
i=0与main-----3中间有的隔一行?
有的打印两次i=0,且与main-----3在同一行?
老师,为什么我的程序静态变量schoolName被读出来了?关键代码如下
public static String schoolName; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", PassWord='" + PassWord + '\'' + ", schoolName=" + schoolName + '}'; } //写对象 oos = new ObjectOutputStream(new FileOutputStream("D:\\student.txt")); Student stu = new Student("marry", 20, "password"); Student.schoolName = "wuyiu"; oos.writeObject(stu); //读对象 ois = new ObjectInputStream(new FileInputStream("D:\\student.txt")); Student stu = (Student)ois.readObject(); System.out.println(stu);
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637