老师,OIS参考模式是不是算一种具体的TCP/IP参考模型,如果不是,那该怎么理解OIS与TCP/IP参考模型的关系呢
请问老师,为什老师的代码在爬取不同网页时,有些会报错(报的错是啥意思呀),有些不会
package com; import java.io.*; import java.net.URL; public class TestURL2 { public static void main(String[] args) throws IOException { URL url=new URL("https://www.baidu.com/"); InputStream is=url.openStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf-8")); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("index.html"),"utf-8")); String line=null; while((line=br.readLine())!=null){ bw.write(line); bw.newLine(); bw.flush(); } bw.close(); } }
package com; import java.io.*; import java.net.URL; public class TestURL2 { public static void main(String[] args) throws IOException { URL url=new URL("https://www.bilibili.com/"); InputStream is=url.openStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf-8")); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("index.html"),"utf-8")); String line=null; while((line=br.readLine())!=null){ bw.write(line); bw.newLine(); bw.flush(); } bw.close(); } }
为什么不能直接在list中删除item?
使用了与老师同样的代码之后却报错(第一次没报错,后来再次运行的时候一直报错),想了好久都没解决(第一张是在D盘里创建了data.txt文件后的,第二张图是没有创建的,不知道为啥会报错找不到文件)
package com.bjsxt; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; public class DataOutputDemo { public static void main(String[] args) { DataOutputStream dos = null; try{ dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("d:/data.txt"))); dos.writeChar('a'); dos.writeInt(10); dos.writeDouble(Math.random()); dos.writeBoolean(true); dos.writeUTF("你好尚学堂"); dos.flush(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(dos != null){ dos.close(); } }catch(Exception e){ e.printStackTrace(); } } } }
那么要怎样才能让这种输出没有乱码呢
package com.bjsxt; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; //处理流 //数据流方便了获取与输出各种数据类型时的转换 //基于字节流输出,会输出乱码 public class DataOutputDemo { public static void main(String[] args) { DataOutputStream dos=null; try { dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("d:/data.txt"))); dos.writeChar('a'); dos.write(10); dos.writeDouble(Math.random()); dos.writeBoolean(true); dos.writeUTF("你好,尚学堂"); dos.flush(); }catch (Exception e) { e.printStackTrace(); }finally { try { if(dos!=null) dos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
老师,请问一下为什么不写关闭语句时字符将不能输出到目标文件中。
package com.bjsxt; import java.io.*; public class LineNumberDemo3 { public static void main(String[] args) { BufferedReader br=null; PrintWriter pw=null; try { br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/sxt.txt"))); pw=new PrintWriter("d:/sxt4.txt"); String temp=""; int i=1; while((temp=br.readLine())!=null){ pw.println(i+""+temp); i++; } } catch (Exception e) { e.printStackTrace(); } } }
老师,我的浏览器为什么打开是这个样子的啊
老师好!
请问为什么我把它变成批处理文件后在黑窗口打开会闪退
老师,接口不是不能实例化吗?为什么这里就可以实例化?
怎么看不了
老师,为什么把条件写在这里就不行呢,就显示空指针异常了呢?
老师,如果在user类中重写toString()方法,然后调用的话,会出现一个异常,java.io.InvalidClassException: com.college.User; local class incompatible: stream classdesc serialVersionUID = 4908776578495550668, local class serialVersionUID = 1941108881546884415,可以讲讲吗
老师,请问为什么继承Mylist后,重写的add方法中element是Object类型的啊
package com.bjsxt; public class MyDoublyLinkedList<E> implements MyList { private Node head; //记录头节点 private Node tail; //记录尾节点 private int size; //记录元素个数 /* 定义双向链表的节点对象 */ class Node<E> { E item; //记录元素 Node<E> prev; //记录前一个节点对象 Node<E> next; //记录后一个节点对象 Node(Node<E> prev, E item, Node<E> next) { this.prev = prev; this.item = item; this.next = next; } } /* 向双向链表中添加元素 */ public void add(Object element){ this.LinkList(element); } /* 将指定对象添加到双向链表的尾部 */ private void LinkList(E element) { //获取尾节点 Node t = this.tail; //创建节点对象 Node<E> node = new Node<>(t, element, null); //将新节点定义为尾节点 this.tail = node; if (t == null) { this.head = node; } else { t.next = node; } this.size++; } /* 通过指定位置获取元素 */ @Override public Object get(int index) { //校验index的合法性 this.checkIndex(index); //根据位置查找节点对象 Node<E> node=this.getNode(index); return node; } /* 对index进行合法性校验 */ private void checkIndex(int index) { if (!(index >= 0 && index < this.size)) { throw new IndexOutOfBoundsException("index:" + index + "size:" + 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; } } /* 获取双向链表中元素的个数 */ @Override public int size() { return this.size; } /* 通过指定位置删除元素 */ @Override public Object 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> my=new MyDoublyLinkedList<>(); my.add("a"); my.add("b"); my.add("c"); System.out.println(my.get(2)); System.out.println(my.size()); 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); }
老师我问一个我自己觉得有点蠢的问题
为什么sort();方法返回值是空,排序后会出现返回值
老师,请问为什么会报错啊
package com.bjsxt; import java.util.Arrays; import java.util.EmptyStackException; /* 自定义栈容器 */ public class MyStack<E> { private Object[] arr; //存放元素的物理结构 private int stackLength = 4; //数组的默认长度 private int size; //记录栈容器的元素个数 private int index = -1; //操作数组下标的指针 //判断栈容器是否为空 public boolean empty() { return this.size==0; } //获取栈顶元素 public E pop() { //如果栈容器中没有元素则抛出异常 if (this.index == -1) { throw new EmptyStackException(); } //记录元素个数 this.index--; //如果有元素,则返回栈顶元素 return (E) this.arr[index--]; } //向栈顶添加元素 public E push(E item) { //初始化数组 this.capacity(); //向数组中添加元素 this.arr[++index] = item; //记录元素个数 this.size++; return item; } //数组初始化或者以1.5倍容量对数组扩容 private void capacity() { //数组初始化 if (this.arr == null) { this.arr = new Object[this.stackLength]; } //以1.5倍对数组扩容 if (this.size - this.stackLength >= 0) { this.stackLength = this.stackLength + this.stackLength >> 1; //>>右位移一位等于除以2 this.arr = Arrays.copyOf(this.arr, this.stackLength); } } public static void main(String[] args) { MyStack<String> myStack=new MyStack<>(); myStack.push("a"); myStack.push("b"); myStack.push("c"); System.out.println(myStack.size); System.out.println(myStack.pop()); System.out.println(myStack.pop()); } }
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637