我已经创建了一个数组列表,并随机插入了几个元素。我试图找出每个元素出现的频率。我尝试了两种方法,两种方法都有效。第一种方法是使用fine.The中可用的直接方法,即,
Collection.frequency(collection,"element");
第二种方法是将ArrayList元素放入HashMap中并找出频率
List<String> elements = new ArrayList<String>();
elements.add("A");
elements.add("B");
下面针对数组中大多数元素的代码适用于元素的n/2倍,但不适用于n/3倍。有谁可以帮我?
class Solution {
public List<Integer> majorityElement(int[] a) {
ArrayList<Integer> arr = new ArrayList<>();
int flag=0;
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j
我有一个数组,我的目标是返回唯一的元素。我编写了对数组进行排序并在ArrayList中存储唯一元素的代码,但是它不是只返回[7],而是返回[2,5,7]。为什么会这样呢?
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
int [] arr = {2, 5, 14, 14, 2, 7, 5};
Arrays.sort(arr);
for (int i =0; i<arr.length-1; i++){
下面的代码用于计算ArrayList中字符串的频率
public static int count (ArrayList<String> c, String str){
int num = 0;
num = Collections.frequency(c, str);
return num;
}
我现在要做的是改进这一点,这样函数就可以接受ArrayList<ArrayList<String>>,并且可以循环遍历Arraylist集合,并计数字符串的出现情况。任何想法都会很好
我有一个值{" 16“、"b”、"c“、"d”、"e“、"16”、"f“、" g”、"16“、”b“}的列表;在这16和b中,我想删除它们的所有条目,我需要输出c,d,e,f,g。下面的程序很好。有更好的解决办法吗?
public class Test {
public static void main(String[] args) {
ArrayList < String > l = new ArrayList < String > ();
String[] str = {
&
如果我需要删除列表中的元素,下面的方法会比使用LinkedList更好吗
int j = 0;
List list = new ArrayList(1000000);
...
// fill in the list code here
...
for (Iterator i = list.listIterator(); i.hasNext(); j++) {
if (checkCondition) {
i.remove();
i = list.listIterator(j);
}
}
LinkedList确实比ArrayList更有效地“移除和
目前,我有两个数组列表显示单词和他们的频率。所以"the“的频率是1,"I”的频率是10,等等。
ArrayList<String> words = new ArrayList<>(Arrays.asList("the", "I", "false","too"));
ArrayList<Integer> frequency = new ArrayList<>(Arrays.asList(1, 10, 5, 7));
Collectio
我在Java中有一个值为ArrayList的
4, 4, 3, 3, 5, 6
我需要找到最常见的价值。如果多个值的出现次数相同,则返回最大的值。
因此,在上面的示例中,我需要返回值4。
private int getFrequentNumber(ArrayList<Integer> arr){
int popular = arr.get(0);
int count = 1;
int tempcount = 0;
int temp = 0;
for(int i = 0; i < arr.size(); i++) {
t
例如,我有一个列表:
ArrayList<Observer> arrlist = new ArrayList<Observer>();
该列表包含观察者对象,但我想知道具有相同值的对象的频率,例如字符串名称。
public class Observer{
private String name;
}
我该怎么做?
我有一个读取txt文件的方法,我需要它获取文本文件中的每个单词,然后按排序顺序迭代它们,而不是删除重复的。我设法让它正常工作,但我想让代码更高效。有人能给我一个阻碍吗,我能做些什么让它更快?不是ArrayList?除了Collections.sort,还有其他方法来排序吗?
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
ArrayList<String> p = new ArrayList<String>();
String line;
in
我有一个颜色及其出现频率的ArrayList。我的程序应该计算这些项目的重新排序,使两个相等的砖块之间的最小距离最大化。
例如,给定由4*brick 1 (x)、3*brick 2 ( y )和5*brick 3 (z)组成的输入,一个正确的输出应该是:。
我的代码没有产生好的解决方案。特别是,有时在最后有两块相等的砖,这是最坏的情况。
import java.util.ArrayList;
import java.util.Collections;
public class Calc {
// private ArrayList<Wimpel> w = new Arra
我正在尝试生成瓷砖,这是一个使用TileCreator的多边形集合。我使用像tileCreator.renderData(),tileCreator.saveAll()这样的方法。这两种方法都工作得很好。但是saveAll()方法需要花费大量时间将图像放入文件系统中。
TileCreator tileCreator= new TileCreator(
directory,
zoomLevel,
new TileRenderer[] { new ValueAsColoredSh
我一直在研究这个Huffman树构建器:
// variable al is an array list that holds all the different characters and their frequencies
// variable r is a Frequency which is supposed to be the roots for all the leaves which holds a null string and the frequencies of the combined nodes removed from the priority queue
pub
我遇到了一些我必须为我的课程编写的代码的麻烦。
我不得不写一个随机数生成器,它对数字1到50,一万次进行遍历,然后只打印出出现频率最高的前15个数字。除了打印出最高的15个之外,我已经成功地做好了所有的事情。
以下是我的完整代码块
package section4;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
Random rand = new Random();
int freq[] = new int[51];