About Question enthuware.ocpjp.v7.2.1471 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocpjp.v7.2.1471 :

Post by ETS User »

In Eclipse this error: Multiple markers at this line
- The type TreeSet is not generic; it cannot be parameterized with arguments
<Integer>
- The type TreeSet is not generic; it cannot be parameterized with arguments
<Integer>
Why?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1471 :

Post by admin »

It is difficult to indentify issues with IDE. Please use command line compilation to see what is the exact error.

sinapse
Posts: 27
Joined: Sat Aug 31, 2013 5:38 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1471 :

Post by sinapse »

Are you using the jdk7 ?

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1471 :

Post by codecodecode67 »

I understand the solution, but why is this:
Further, since the subset is created using a range (fromElement to toElement), the element that you are inserting must fall within that range. Otherwise an IllegalArgumentException is thrown with a message "key out of range."
the case? Why must it be within that range, why does it matter that the subset is created using a range? How is a subset created from a range different from a regular treeset? (I'm trying to rephrase my question a couple of times to make it more clear what I don't get)

Edit: Does the insertion have to be within the range of the subset because the subset is backed by the actual set, so if it's outside of the range of the subset, there won't be a valid place to put it in the real subset?

Does this mean that this is also valid in the same way for NavigableMap implementations and its subMap(...) methods?

Thanks

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1471 :

Post by admin »

codecodecode67 wrote:I understand the solution, but why is this:
Further, since the subset is created using a range (fromElement to toElement), the element that you are inserting must fall within that range. Otherwise an IllegalArgumentException is thrown with a message "key out of range."
the case? Why must it be within that range, why does it matter that the subset is created using a range? How is a subset created from a range different from a regular treeset? (I'm trying to rephrase my question a couple of times to make it more clear what I don't get)

Edit: Does the insertion have to be within the range of the subset because the subset is backed by the actual set, so if it's outside of the range of the subset, there won't be a valid place to put it in the real subset?
Correct.
Does this mean that this is also valid in the same way for NavigableMap implementations and its subMap(...) methods?

Thanks
Correct.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1471 :

Post by sir_Anduin@yahoo.de »

How to understand "range" correctly.
What if I have the following code:
What defines "range" here?

Code: Select all

public class Main2 {

    public static void main(String[] args) {

        TreeSet<Animal> s = new TreeSet<Animal>();
        TreeSet<Animal> subs = new TreeSet<Animal>();

        Animal e = new Animal();
        s.add(e);
        s.add(new Animal());
        s.add(new Animal());
        Animal e1 = new Animal();
        s.add(e1);

        subs = (TreeSet) s.subSet(e, true, e1, true);
        subs.add(new Animal());
        System.out.println(s + " " + subs);


    }
class Animal implements Comparable<Animal> {
    @Override
    public int compareTo(Animal o) {
        return 0;
    }
}

}


admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1471 :

Post by admin »

s points to a TreeSet. A TreeSet is a sorted set, which means its elements are stored in a sorted fashion. For example, if you store numbers 1 to 9 in a TreeSet, they will be kept in sorted order i.e. from 1 to 9. Now, if you create a subset using this set using subSet(3, true, 6, true); the range of the subset is 3 to 6. You cannot insert number 2 or 7 in this subset because they out of that range.

You should go through the JavaDoc API of this method for further details and try out a few examples to get a better understanding.

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests