「javaskip」javaskiplist应用
本篇文章给大家谈谈javaskip,以及javaskiplist应用对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java中统计JTextArea中的行数有两个方法的问题!
- 2、java中怎么把文件读取指针指向上一个字符啊?
- 3、java中使用skipByts有什么意义
- 4、java中的skip list
- 5、JAVA急救啊!! 1.System.in.skip(2);是什么意思? 2.还有try,catch用来干什么的? 3.(int)+ch什么意思
- 6、java中seek()的用法
java中统计JTextArea中的行数有两个方法的问题!
首先看下,JTExtArea的其中一个构造方法
public JTextArea(int rows, int columns)
构造具有指定行数和列数的新的空 TextArea。创建默认模型,初始字符串为 null。
参数:rows - 行数 = 0 columns - 列数 = 0
这个里面设置的rows,可以通过getRows()这个方法获取到的。
换言之,你可以把rows想象成高,把columns想成宽(再不受其他组件影响的前提下,pack
方法显示面板,就可以看到你设置这2个参数的作用了)
getLineCount(),返回的是你文本区有多少行数据(输入了几次回车符号!)
多想,多看(API),多试(验证自己的想法)
有问题再追问,good luck!~
java中怎么把文件读取指针指向上一个字符啊?
就你写的那个流,没那回退的方法,我到是有个思路,就是你每次read的时候读取了多少个字节,累加起来,之后在需要回退的时候,尝试调用reset方法,再调用skip方法,把之前读取统计的字节数传给skip方法,可是skip也不是万试万灵,有时你要跳过的字节数大了,她就会只跳了一些字节数,这个表示还没找到原因,只是一次项目中遇到过,后来用循环代替了skip
java中使用skipByts有什么意义
seek(long a)是定位文件指针在文件中的位置。参数a确定读写位置距离文件开头的字节个数,比如seek(0)就是定位文件指针在开始位置。是绝对定位。
skipBytes()是指在文件中跳过给定数量的字节。是相对定位。
对于seek方法,拥有skipBytes( )的功能,但seek( )在使用过程非常影响系统的开销。
java中的skip list
首先,给你个实现类:/*************************** SkipList.java *********************/import java.util.Random;public class SkipListT extends Comparable? super T {
private int maxLevel;
private SkipListNodeT[] root;
private int[] powers;
private Random rd = new Random();
SkipList() {
this(4);
}
SkipList(int i) {
maxLevel = i;
root = new SkipListNode[maxLevel];
powers = new int[maxLevel];
for (int j = 0; j maxLevel; j++)
root[j] = null;
choosePowers();
}
public boolean isEmpty() {
return root[0] == null;
}
public void choosePowers() {
powers[maxLevel-1] = (2 (maxLevel-1)) - 1; // 2^maxLevel - 1
for (int i = maxLevel - 2, j = 0; i = 0; i--, j++)
powers[i] = powers[i+1] - (2 j); // 2^(j+1)
}
public int chooseLevel() {
int i, r = Math.abs(rd.nextInt()) % powers[maxLevel-1] + 1;
for (i = 1; i maxLevel; i++)
if (r powers[i])
return i-1; // return a level the highest level;
return i-1; // return the highest level;
}
// make sure (with isEmpty()) that search() is called for a nonempty list;
public T search(T key) {
int lvl;
SkipListNodeT prev, curr; // find the highest nonnull
for (lvl = maxLevel-1; lvl = 0 root[lvl] == null; lvl--); // level;
prev = curr = root[lvl];
while (true) {
if (key.equals(curr.key)) // success if equal;
return curr.key;
else if (key.compareTo(curr.key) 0) { // if smaller, go down,
if (lvl == 0) // if possible
return null;
else if (curr == root[lvl]) // by one level
curr = root[--lvl]; // starting from the
else curr = prev.next[--lvl]; // predecessor which
} // can be the root;
else { // if greater,
prev = curr; // go to the next
if (curr.next[lvl] != null) // non-null node
curr = curr.next[lvl]; // on the same level
else { // or to a list on a lower level;
for (lvl--; lvl = 0 curr.next[lvl] == null; lvl--);
if (lvl = 0)
curr = curr.next[lvl];
else return null;
}
}
}
}
public void insert(T key) {
SkipListNodeT[] curr = new SkipListNode[maxLevel];
SkipListNodeT[] prev = new SkipListNode[maxLevel];
SkipListNodeT newNode;
int lvl, i;
curr[maxLevel-1] = root[maxLevel-1];
prev[maxLevel-1] = null;
for (lvl = maxLevel - 1; lvl = 0; lvl--) {
while (curr[lvl] != null curr[lvl].key.compareTo(key) 0) {
prev[lvl] = curr[lvl]; // go to the next
curr[lvl] = curr[lvl].next[lvl]; // if smaller;
}
if (curr[lvl] != null key.equals(curr[lvl].key)) // don't
return; // include duplicates;
if (lvl 0) // go one level down
if (prev[lvl] == null) { // if not the lowest
curr[lvl-1] = root[lvl-1]; // level, using a link
prev[lvl-1] = null; // either from the root
}
else { // or from the predecessor;
curr[lvl-1] = prev[lvl].next[lvl-1];
prev[lvl-1] = prev[lvl];
}
}
lvl = chooseLevel(); // generate randomly level
newNode = new SkipListNodeT(key,lvl+1); // for newNode;
for (i = 0; i = lvl; i++) { // initialize next fields of
newNode.next[i] = curr[i]; // newNode and reset to newNode
if (prev[i] == null) // either fields of the root
root[i] = newNode; // or next fields of newNode's
else prev[i].next[i] = newNode; // predecessors;
}
}
}其次,再给你一个比较详细的参考:
JAVA急救啊!! 1.System.in.skip(2);是什么意思? 2.还有try,catch用来干什么的? 3.(int)+ch什么意思
1, System.in.skip(2);是跳过输入流的2个字节,简单点你可以理解为不要最前面的2个字符
2 try cacth是用来处理程序运行过程中发生的exception的。程序员积极的处理从而可以保证程序面对意想不到的事情的时候能够健壮运行
3 (int) ch,如果ch是字符型,那就是求字符类型ch的ASCII码值,如果是数字类型,就是将ch强制转换为int类型。
java中seek()的用法
写段代码你看一下吧,用于从文件指定的位置开始读取,一般的下载工具都有断点续传功能,比如读取某个文件读取了一半,取消下载了,下次再下载的时候,从断点的位置继续下载,而不是重新下载文件,使用这个方法可以做到
public class Test2 {
public static void main(String[] args) throws Exception {
String filepath = "E:/test.exe";
String outFile = "E:/copy.exe";
long pos = firstRead(filepath, outFile);
continueRead(filepath, outFile, pos);
}
/**
* 第一次只读取文件的一半,到目标文件
*/
public static long firstRead(String filepath, String out) throws Exception {
RandomAccessFile file = new RandomAccessFile(filepath, "r");
long fileLen = file.length();
FileOutputStream outStream = new FileOutputStream(out);
int sum = 0; // 用于记录当前读取源文件的长度
byte[] cache = new byte[1024];
int len = -1;
while ((len = file.read(cache)) != -1 sum fileLen/2) {
outStream.write(cache, 0, len);
sum += len;
}
outStream.close();
file.close();
return sum; // 返回当前读取源文件的长度
}
/**
* 从源文件指定位置继续读取文件内容,并输出到目标文件
*/
public static void continueRead(String filepath, String out, long pos) throws Exception {
RandomAccessFile file = new RandomAccessFile(filepath, "r");
file.seek(pos);
// 追加到目标文件中
FileOutputStream outStream = new FileOutputStream(out, true);
byte[] cache = new byte[1024];
int len = -1;
while ((len = file.read(cache)) != -1) {
outStream.write(cache, 0, len);
}
outStream.close();
file.close();
}
}
关于javaskip和javaskiplist应用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。