gpt4 book ai didi

java - java中 vector 的使用

转载 作者:行者123 更新时间:2023-11-29 08:16:38 25 4
gpt4 key购买 nike

我一直在研究这个问题,它涉及到马尔可夫链算法在java中的实现。当我为变量 r 分配 vector 元素的值时,它给了我一个错误。 (第 54 行)。错误显示为“需要数组但找到了 java.unit.Vector”。代码粘贴在下面。

import java.io.*;
import java.util.*;

class Chain {
static final int NPREF = 2; // size of prefix
static final String NONWORD = "\n";
// "word" that can't appear
Hashtable statetab = new Hashtable();
// key = Prefix, value = suffix Vector
Prefix prefix = new Prefix(NPREF, NONWORD);
// initial prefix
//Random rand = new Random();

// Chain build: build State table from input stream
void build(BufferedReader in) throws IOException
{
StreamTokenizer st = new StreamTokenizer(in);

st.resetSyntax(); // remove default rules
st.wordChars(0, Character.MAX_VALUE); // turn on all chars
st.whitespaceChars(0, ' '); // except up to blank
while (st.nextToken() != st.TT_EOF)
add(st.sval);
add(NONWORD);
}

// Chain add: add word to suffix list, update prefix
void add(String word)
{
Vector suf = (Vector) statetab.get(prefix);
if (suf == null) {
suf = new Vector();
statetab.put(new Prefix(prefix), suf);
}
suf.addElement(word);
prefix.pref.removeElementAt(0);
prefix.pref.addElement(word);
}

// Chain generate: generate output words
void generate(int nwords, PrintWriter out, Vector <Integer> random_num)
{
prefix = new Prefix(NPREF, NONWORD);
for (int i = 0; i < nwords; i++) {
Vector s = (Vector) statetab.get(prefix);
if (s == null) {
System.err.println("Markov: internal error: no state");
System.exit(1);
}

final int r = (random_num.get(i) % s.size());
String suf = (String) s.elementAt(r);
if (suf.equals(NONWORD))
break;
out.println(suf);
prefix.pref.removeElementAt(0);
prefix.pref.addElement(suf);
}
}
}

class Prefix {
public Vector pref; // NPREF adjacent words from input
static final int MULTIPLIER = 31; // for hashCode()

// Prefix constructor: duplicate existing prefix
Prefix(Prefix p)
{
pref = (Vector) p.pref.clone();
}

// Prefix constructor: n copies of str
Prefix(int n, String str)
{
pref = new Vector();
for (int i = 0; i < n; i++)
pref.addElement(str);
}

// Prefix hashCode: generate hash from all prefix words
public int hashCode()
{
int h = 0;

for (int i = 0; i < pref.size(); i++)
h = MULTIPLIER * h + pref.elementAt(i).hashCode();
return h;
}

// Prefix equals: compare two prefixes for equal words
public boolean equals(Object o)
{
Prefix p = (Prefix) o;

for (int i = 0; i < pref.size(); i++)
if (!pref.elementAt(i).equals(p.pref.elementAt(i)))
return false;
return true;
}

}

class Markov {
static final int MAXGEN = 10000; // maximum words generated
static Vector <Integer> random_num = new Vector <Integer>();

static BufferedReader random_reader;
public static void main(String[] args) throws IOException
{

BufferedReader in = new BufferedReader (new FileReader ("../alice30.txt"));
PrintWriter out = new PrintWriter (new FileWriter ("../output/markov_java_out.txt"));
BufferedReader random_reader = new BufferedReader (new FileReader ("../random_num.txt"));
String s;
s= random_reader.readLine();
while(s != null)
{
int element = Integer.parseInt(s.trim());
random_num.addElement(new Integer(element));
s = random_reader.readLine();
}

Chain chain = new Chain();
int nwords = MAXGEN;

chain.build(in);

in.close();
chain.generate(nwords,out, random_num);
out.close();
}
}

最佳答案

您是说 random_num.elementAt(i) 吗?

关于java - java中 vector 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com