gpt4 book ai didi

java - 将 for 循环外的值传递给变量 - java::processing

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

我正在研究如何将值词及其对应的计数传递到 for 循环之外的变量中。我的代码目前是:

import java.util.Iterator; 
import java.util.*;


import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;

ImmutableMultiset<String> top = null;

void setup() {
size(800, 480);
smooth();

String[] data = loadStrings("data/data.txt");

ImmutableMultiset<String> myMultiset = ImmutableMultiset.copyOf(data);

top = Multisets.copyHighestCountFirst(myMultiset);

Iterator it = top.entrySet().iterator();

for (int i = 0; (i < 5) && it.hasNext(); i++) {
Multiset.Entry entry = (Multiset.Entry) it.next();

String word = (String) entry.getElement();
int count = entry.getCount();

System.out.println(word + " -> " + count);
}
}

我需要这些值来将它们应用于我的 draw() 函数中的条形图。

最佳答案

您可以在您的类中而不是在您的增强循环中本地声明变量。

例如

private String word;
private int count;

并使用 getters 调用它们

例如

public String getWord(){ return word; }

关于java - 将 for 循环外的值传递给变量 - java::processing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282525/

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