gpt4 book ai didi

java - 使用 Java 反射访问类的字段

转载 作者:行者123 更新时间:2023-12-01 21:26:24 25 4
gpt4 key购买 nike

我正在尝试访问一个包含 ID、名称、文本和单词列表的文档类。我尝试将我拥有的 id 与 ids 进行比较,找到后获取附加到该 id 的单词列表以找到确切的单词。我正在尝试使用 java 反射,但无法让它工作?
非常感谢任何帮助。

public class Doc {
private static int documentID;
private static Doc docInstance = null;
private String documentText;
private ArrayList<String> listOfTokens;
static int docCount = 0;

public Doc() {
documentID = 0;
listOfTokens = new ArrayList<String>();
tokFreq = 0;
docCount++;
}

public static Doc getDocInstance() {
if (docInstance == null) {
docInstance = new Doc();
}
return docInstance;
}

public ArrayList<String> getListOfTokens() {
return listOfTokens;
}
}

我正在尝试这个

public static void createDocumentVector(TreeMap<Integer,Integer> 
documentVector, TreeMap<String, ArrayList<Integer>>qm, int N) throws
NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException, InstantiationException, NoSuchMethodException,
InvocationTargetException
{
int eachDoc = 0;

Collection<String> allKeys = qm.keySet();
ArrayList<Integer> l1 = new ArrayList<Integer>();
boolean addedTerm = false;

/**
Obtain an Iterator for Collection
*/
Iterator<String> itr = allKeys.iterator();
String key;
int termFrequency = 0;
int documentFrequency = 0;

/**
Iterate through TreeMap values iterator
*/
while(itr.hasNext())
{
key = (String)itr.next();
Integer LL = 0;
l1 = qm.get(key); // Returns value of that key
for (int k = 0; k < l1.size(); k++)
{
LL = l2.get(k);

Doc obj = new Doc();
Class<? extends Doc> docOb = obj.getClass();
Field field1 = docOb.getDeclaredField("documentID");
field1.setAccessible(true);
Field field2 = docOb.getDeclaredField("listOfTokens");
field1.setAccessible(true);

if (field1.isAccessible()) {
Method setID = docOb.getDeclaredMethod("setDocumentID", new Class[]{int.class});
setID.setAccessible(true);
setID.invoke(docOb, LL);
}

Method listTock = docOb.getMethod("getListOfTokens");
ArrayList<String> per = (ArrayList<String>) listTock.invoke(docOb, null);
for (String tock : per) {
if(tock.equals(key)) {
termFrequency++;
}
}

documentFrequency = l1.size();
eachDoc.add(getTFIDF(termFrequency, documentFrequency, N));
documentVector.put(eachDoc, LL);
addedTerm = true;
}


}
}

我收到此错误

Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

最佳答案

TreeMap<List<Integer>,Integer>由于 javadoc 中的此解释,导致错误。

The map is sorted according to the {@linkplain Comparable natural ordering} of its keys

但是List没有实现Comparable界面。所以你不能使用List<Integer>作为 TreeMap 中的键。

我的英语很差,希望你能理解!

关于java - 使用 Java 反射访问类的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112278/

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