Ten program pokazuje błąd czasu kompilacji .. niedozwolony start typu dla (Integer it: arr), dlaczego pokazuje błąd? [duplikować]

Dec 19 2020
import java.util.*;
class Test
{
    Set<Integer> st = new HashSet<Integer>();

    Integer arr[]={1,1,2,2,4,8};

    for (Integer it : arr)
    {
        st.add(it);
        System.out.println(st);
    }

    public static void main(String ar[]){

        System.out.println("Main");


}}

Odpowiedzi

Verity Dec 19 2020 at 16:27

Umieść pętlę for wewnątrz głównej metody, na przykład:

public static void main(String ar[]) {

    Set<Integer> st = new HashSet<Integer>();

    Integer arr[]={1,1,2,2,4,8};

    for (Integer it : arr) {
        st.add(it);
        System.out.println(st);
    }


}