接收要列出的字符串时出错

Error with receiving strings to list

这是一个程序,用于接收 n 个字符串以列出并查找每个字符串的重复项计数,并在映射中打印字符串和重复项数。

我收到这个错误,但我找不到问题所在!

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Q1.main(Q1.java:16)

代码如下:

import java.util.*;

public class Q1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<String,Integer> m = new HashMap<String,Integer>();
        List<String> l = new ArrayList<String>();
        int n = sc.nextInt();
        for(int i = 1; i <= n; i++) {
            l.add(sc.nextLine());
        }
        int count=1;
        for(int i = 0; i < l.size(); count = 1) {
            for(int j = 1; i < l.size(); j++){
                if(l.get(j)==l.get(i)){
                    l.remove(j);
                    count++; 
                }
            }
            m.put(l.get(i),count);
        }
        for(int i = 0;i < l.size(); count = 1) {
            System.out.println(m.get(i));
        }
    }
}

你没有递增 i :

for(int i=0;i<l.size();count=1){

同样在下面的代码中:

`for(int j = 1; i < l.size(); j++){`

i 的值是固定的,j 会随着 i < l.size()(始终)无限递增,这会导致 IndexOutOfBoundsException。