📄 strvect.java
字号:
package Calc;
import java.util.*;
public class StrVect {
Vector v = new Vector();
int capacity = -1;
int recno = -1;
int row = -1, col = 1;
boolean FIFO = true;
public StrVect() { }
public StrVect(int r) {
row = (r <= 0 ? -1 : r);
col = 1;
capacity = row;
}
public StrVect(int r, int c) {
row = (r <= 0 ? -1 : r);
col = (c <= 0 ? 1 : c);
capacity = (row == -1 ? -1 : row * col);
}
public void setFIFO(boolean b) { FIFO = b; } //先进先出
public int capacity() { return capacity; } //容量,最大允许的变量个数
public int size() { return v.size(); } //数量,现已使用的个数
public int firstof() { return (isempty() ? -1 : 0); }//第一行,0行
public int lastof() { return v.size()/col - 1; } //最后的索引值,第col()行,lastof() = col() - 1
public int recno() { return recno; } //当前记录号,从firstof()到lastof()
public int row() { return v.size()/col; } //行数,索引值从0开始,行列数从1开始,-1则是不限制
public int col() { return col; } //列数,从1开始的正整数
public boolean isempty() { //是否为空
if (v.size() > 0) { return false; }
else { return true; }
}
public boolean aof() { //是否位于有效记录之前
if (isempty()) { return true; }
else if (recno < 0) { return true; }
else { return false; }
}
public boolean fof() { //是否位于有效记录之后
if (isempty()) { return true; }
else if (recno > lastof()) { return true; }
else { return false; }
}
public boolean bof() { //0号记录
if (aof() || fof()) { return false; }
else if (recno == 0) { return true; }
else { return false; }
}
public boolean eof() { //尾号记录
if (aof() || fof()) { return false; }
else if (recno == lastof()) { return true; }
else { return false; }
}
public boolean go(int r) {
recno = r;
if (inrow(recno)) { return true; }
else { return false; }
}
public boolean skip(int r) {
return go(recno + r);
}
public int add() { //增加一条空记录,col() 个变量,返回记录号
for (int i = 0; i < col; i++) { v.addElement(""); }
trimtosize();
recno = lastof();
return recno;
}
public int add(String s0, String s1) {
if (col != 2) { return -1; }
v.addElement(s0);
v.addElement(s1);
trimtosize();
recno = lastof();
return recno;
}
public int insert(int r) { //插入
if (inrow(r)) {
for (int i = 0; i < col; i++) { v.insertElementAt("", r * col); }
trimtosize();
recno = r;
return r;
} else {
add();
return lastof();
}
}
public boolean delete(int r) { //删除记录
if (inrow(r)) {
for (int i = 0; i < col; i++) { v.removeElementAt(r * col); }
trimtosize();
recno = (r > lastof() ? lastof() : r);
return true;
} else {
return false;
}
}
public boolean remove(int r) { //删除列
return delete(r);
}
public void removeall() { //删除全部列
v.removeAllElements();
}
public boolean trimtosize() { //超容量则将前面的删除
boolean l = true;
while (v.size() > capacity && capacity > 0) {
v.removeElementAt((FIFO ? 0 : v.size()-1));
l = false;
}
return l;
}
public boolean setsize(int i) { //设置容量
if (i <= 0) {capacity = -1; return true; }
else if (i >= capacity) { capacity = i * col; return true; }
else if (i < capacity) { capacity = i * col; return trimtosize(); }
return false;
}
public boolean insize(int i) { //是否在容量范围内
if (i >= 0 && i < v.size()) { return true; }
else { return false; }
}
public boolean inrow(int i) { //是否在索引范围内
if (i >= 0 && i < row()) { return true; }
else { return false; }
}
public boolean incol(int i) { //是否在列范围内
if (i >= 0 && i < col()) { return true; }
else { return false; }
}
public boolean set(String s, int i) { //改值
if (insize(i)) { recno = i/col; v.setElementAt(s, i); return true; }
else { return false; }
}
public boolean set(String s, int r, int c) { //改值
if (inrow(r) && incol(c)) { return set( s, (r * col + c) ); }
else { return false; }
}
public boolean set(String s0, String s1, int r) {//改值,两列的赋值
if (inrow(r) && col == 2) {
recno = r;
v.setElementAt(s0, r * col + 0);
v.setElementAt(s1, r * col + 1);
return true;
} else { return false; }
}
public String get(int i) { //获取值
if (insize(i)) { recno = i/col; return ((String) v.elementAt(i)); }
else { return null; }
}
public String get(int r, int c) { //获取值
if (inrow(r) && incol(c)) { return get(r * col + c); }
else { return null; }
}
public boolean contains(String s) { //是否包含值
for (int i = 0; i < v.size(); i++) {
if (get(i).equals(s)) { return true; }
}
return false;
}
public int indexof(String s) { //从前搜索
for (int i = 0; i < v.size(); i++) {
if (get(i).equals(s)) { return i; }
}
recno = row();
return -1;
}
public int indexof(String s, int index) { //从指定位置搜索
if (!insize(index)) { return -1; }
for (int i = index; i < v.size(); i++) {
if (get(i).equals(s)) { return i; }
}
recno = row();
return -1;
}
public int indexof(String s, int r, int c) { //从指定行开始,搜索指定列,返回行号
if (!inrow(r) || !incol(c)) { return -1; }
for (int i = (r * col + c); i < v.size(); i+=c) {
if (get(i).equals(s)) { return recno; }
}
recno = row();
return -1;
}
public int lastof(String s) { //从后搜索
for (int i = v.size() - 1; i >= 0; i--) {
if (get(i).equals(s)) { return i; }
}
recno = -1;
return -1;
}
public int lastof(String s, int index) { //从后搜索至指定位置
if (!insize(index)) { return -1; }
for (int i = v.size() - 1; i >= index; i--) {
if (get(i).equals(s)) { return i; }
}
recno = -1;
return -1;
}
public int lastof(String s, int r, int c) { //从后搜索至指定位置,搜索指定列,返回行号
if (!inrow(r) || !incol(c)) { return -1; }
for (int i = lastof() * col + c; i >= r * col + c; i-=col) {
if (get(i).equals(s)) { return recno; }
}
recno = -1;
return -1;
}
public int occur(String s, int r0, int r1, int c) { //搜索指定范围,确定出现次数
if (!inrow(r0) || !inrow(r1) || !incol(c)) { return -1; }
int oi = 0;
for (int i = r0 * col + c; i <= r1 * col + c; i+=col) {
if (get(i).equals(s)) { oi++; }
}
return oi;
}
public int occur(String s, int r0, int r1) { //搜索指定范围,确定出现次数
if (!inrow(r0) || !inrow(r1)) { return -1; }
int oi = 0;
for (int i = r0 * col; i < (r1 + 1) * col; i++) {
if (get(i).equals(s)) { oi++; }
}
return oi;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -