-3

It's really a basic questions, but still was unable to find an answer to it. i was just messing around with some basic Collection methods (like the below example) and yes, it works perfectly as it should be. Problem isn't really in this code, its more about eclipse i think. Cuz in eclipse everytime when i create smth with "list" word like "List list = new ArrayList();" i got an error. Or when i create it in some other, working way i cant access Collections. options. only Collections.combine i think. sort and all the other isnt really found and dont work ( so i switched to netbeans to make this code work and practise ) and it works perfectly. but how to make this work in eclipse?? p. s. i have imported all the needed things.

ArrayList<String> list2 = new ArrayList<String>();
    String[] programs = {"eclipse", "netbeans", "codeblocks", "visual studio"};

    ArrayList<String>convertedlist = new ArrayList(Arrays.asList(programs));

            convertedlist.add("\nnotepad++");
    output(convertedlist);
            System.out.println("\nsorted list");                
            Collections.sort(convertedlist);
            output(convertedlist);
            System.out.println("\nreverse order");
            Collections.sort(convertedlist, Collections.reverseOrder());
            output(convertedlist);
            System.out.println("\nsecond array element");
            programs = convertedlist.toArray(new String[convertedlist.size()]);
            System.out.println(programs[1]);

            System.out.println("\nfilled list");
            Collections.fill(convertedlist, "L");
            output(convertedlist);
            System.out.println("\nadded all to list2");
            Collections.addAll(list2, programs);
            output(list2);
            System.out.println("\nfrequency of word net beans in list2");
            System.out.println(Collections.frequency(list2, "netbeans

            public static void output(ArrayList<String> list){
    for(String a: list)
        System.out.printf("%s ", a);
4
  • 7
    "i got an error" tells us nothing about what the error is.. nor does "and dont work". Ideally, you should provide a short but complete program, nicely formatted, with the errors you're running into.
    – Jon Skeet
    Commented Mar 2, 2015 at 13:47
  • you are probably not pointing to the correct JDK, eclipse has a number of running environment, right click your project and make your JDK point to the complete jdk package, you will find it easily try it..Change the existing running environment to jdk and test it. Commented Mar 2, 2015 at 13:49
  • 1
    Or, more likely, he's getting a raw-type warning. Commented Mar 2, 2015 at 13:49
  • its either as @chrylis said , or you are just not importing right List (import java.util.List;)
    – nafas
    Commented Mar 2, 2015 at 13:54

1 Answer 1

0

You are missing brackets and quote symbol after before output method. I tested your code and I do not seem to get any problems. I had a warning, which said:

Blockquote Description Resource Path Location Type ArrayList is a raw type. References to generic type ArrayList should be parameterized Main.java /Test/src line 11 Java Problem Blockquote

It can be fixed by doing:

ArrayList<String>convertedlist = new ArrayList<String>(Arrays.asList(programs));

Here is full code let me know if it gives any errors:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main {

    public static void main(String[] args) {
        ArrayList<String> list2 = new ArrayList<String>();
        String[] programs = {"eclipse", "netbeans", "codeblocks", "visual studio"};

        ArrayList<String>convertedlist = new ArrayList<String>(Arrays.asList(programs));

        convertedlist.add("\nnotepad++");
        output(convertedlist);
        System.out.println("\nsorted list");                
        Collections.sort(convertedlist);
        output(convertedlist);
        System.out.println("\nreverse order");
        Collections.sort(convertedlist, Collections.reverseOrder());
        output(convertedlist);
        System.out.println("\nsecond array element");
        programs = convertedlist.toArray(new String[convertedlist.size()]);
        System.out.println(programs[1]);

        System.out.println("\nfilled list");
        Collections.fill(convertedlist, "L");
        output(convertedlist);
        System.out.println("\nadded all to list2");
        Collections.addAll(list2, programs);
        output(list2);
        System.out.println("\nfrequency of word net beans in list2");
        System.out.println(Collections.frequency(list2, "netbeans"));

    }
    public static void output(ArrayList<String> list){
        for(String a: list)
            System.out.printf("%s ", a);
    }

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.