修改阵列列表内部数组中的单个元素



我必须为一个类的代码编写一个代码,该类别计算输入文件中字符的出现,然后对其进行分类,然后我选择通过创建一个arraylist来做到这一点每个对象[]有两个元素,即字符和出现的数量。

我试图递增代表出现数量的整数,我只是无法工作

我目前的尝试看起来像这样:

  for(int i=0;i<=text.length();i++) {
        if(freqlist.contains(text.charAt(i))) {
            freqlist.indexOf(text.charAt(i))[1]=freqlist.get(freqlist.indexOf(text.charAt(i)))[1]+1;
        }
    }

文本只是一个包含所有输入文件的字符串

freqlist较早声明为

List<Object[]> freqlist=new ArrayList<Object[]>();

所以,我想知道如何递增或修改数组中的数组的元素

通常,您的程序中有3个错误可以阻止其工作。它无法使用,因为For循环具有i<=text.length(),并且应该是i < text.length(),否则您将有例外。第二个错误是您使用freqlist.contains(...),其中您假设对象数组的两个元素都是相同的,或者换句话说,数组是相等的,这是错误的假设。第三个错误是使用freqlist.indexof(...(,它再次依赖数组平等。尽管此数据结构List<Object[]>对任务效率低下,但我做了一个示例。最好使用Map<Character,Integer>。在这里是:

import java.util.ArrayList;
import java.util.List;
class Scratch {
    public static void main(String[] args) {
        String text = "abcdacd";
        List<Object[]> freqlist= new ArrayList<>();
        for(int i=0;i < text.length();i++) {
            Object [] objects = find(freqlist, text.charAt(i));
            if(objects != null) {
                objects[1] = (Integer)objects[1] +1;
            } else {
                freqlist.add(new Object[]{text.charAt(i), 1});
            }
        }
        for (Object[] objects : freqlist) {
            System.out.println(String.format(" %s  =>  %d", objects[0], objects[1]));
        }
    }
    private static Object[] find(List<Object[]> freqlist, Character charAt) {
        for (Object[] objects : freqlist) {
            if (charAt.equals(objects[0])) {
                return objects;
            }
        }
        return null;
    }
}

我这样做的方式首先解析文件并将其转换为字符数组。然后将其发送到Charcounter((方法,该方法将计算文件中字母发生的次数。

/**
     * Calculate the number of times a character is present in a character array
     *
     * @param myChars An array of characters from an input file, this should be parsed and formatted properly
     *                before sending to method
     * @return A hashmap of all characters with their number of occurrences; if a
     *                letter is not in myChars it is not added to the HashMap
     */
    public HashMap<Character, Integer> charCounter(char[] myChars) {
        HashMap<Character, Integer> myCharCount = new HashMap<>();
        if (myChars.length == 0) System.exit(1);
        for (char c : myChars) {
            if (myCharCount.containsKey(c)) {
                //get the current number for the letter
                int currentNum = myCharCount.get(c);
                //Place the new number plus one to the HashMap
                myCharCount.put(c, (currentNum + 1));
            } else {
                //Place the character in the HashMap with 1 occurrence
                myCharCount.put(c, 1);
            }
        }
        return myCharCount;
    }

如果您使用Java 8进行分组:

Map<String, Long> map = dummyString.chars() // Turn the String to an IntStream
        .boxed() // Turn int to Integer to use Collectors.groupingBy
        .collect(Collectors.groupingBy(
                Character::toString,     // Use the character as a key for the map
                Collectors.counting())); // Count the occurrences

现在您可以对结果进行排序。

最新更新