G Batra

Greenhorn
+ Follow
since Aug 09, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by G Batra

Hi Roel,
When I posted the question about add(E) in subList I was pretty sure I was overlooking something and yes you were right. The add(int, E) method in the SubList inner class gets executed as it overrides the same method from AbstractList.
So call sequence: AbstractList.add(E) -> AbstractList.add(int, E) (Now overridden by SubList.add(int, E)) [Please ignore my static method style but you get the point.. ]

After carefully going through your post at least twice it has started to make some sense. Thank you for taking time out and putting effort in such an extraordinary post.
Exceptional explanation.

Thank you again for resolving yet another post.

Thank you Henry as well.




1. I think I should have been more specific here. Sorry for not mentioning clearly but what I meant was the subclass returned from the ArrayList.subList (int, int) method and not the abstractList itself. For some reason I thought it was implied but anyways. There's no add method in the SubList inner class of ArrayList, Roel:



add method gets inherited from the way up from the Collection (I) --> AbstractList (AbstractClass). The super AbstractList of inner class SubList provides an implementation of the add method which throws the exception. And Collection is an interface. So, I am really not sure which add method implementation is being invoked here? The exact method we're interested in is add(E) because add(int, E) is implemented in the inner class SubList.

2. Two cases:
Case 1: Simple case when add(E) is invoked on the subList reference. The element gets added at the end of the list in both the lists. Works fine. No RTE thrown. Behavior however, is inconsistent with NavigableSet and Map. But as Roel pointed out it is how the method was implemented.

Case 2: When add(E) is called on the backed list. RTE ConcurrentModificationException thrown. Why? The method call was just an attempt to add an element at the end of the list. Looks like this operation is independent of any impact on the sublist. Where's the concurrentModification? The excerpt from the Javadoc of subList method is vague:

"..... The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list......."


And Roel, if I try to add an element at position 5 on the sublist despite the list size of being 4, it throws RTE IndexOutOfBoundsException.

Thank you for the Cow and ofcourse your responses.
Hello,

I was working through methods of ArrayList class and the method sublist didn't work the way I expected. Need a little help to understand this method's behavior. Two questions in this thread:

1. When subList(int, int) method is called, it returns an instance of AbstractList. Now, if I try to add an element in this sublist by calling add method on AbstractList instance I don't get an exception which is thrown in the AbstractList class in turn the element gets added. Not sure how this is working?

AbstractList's add method calls this method:



2. What's the point of ArrayList's subList method considering the fact that after its invocation:
- It doesn't allow structural modification to the backed List?

2.1 Also, why adding an element to the end of the subList works? Shouldn't the bounds of subList limit the addition of new elements? Think throwing ConcurrentModificationException when an element is added outside its bounds in the sublist make
more sense.

Adding supporting code snippet:


Output of above code:
I think talking about the issues, actually put some sense into place and widens the visibility.

Thank you Roel and Paul for the explanation and Analogies..

Thank you for your reply Roel.

Is the usability of the statement drives the validity of it? I mean, if the statement is useful in any way, then the statement becomes a valid statement. If it isn't then it is NOT a valid statement.

Just wanted to know, why Java doesn't enforce the method return values to be assigned to appropriate variable in the calling method? Does it not make the return values as "useless".?
Hello,
Could anyone please explain to me why Java behave differently on Line 1 and Line 2 in this code



Why Line 1 doesn't compile and Line 2 does?
Thanks Sergej. That JLS link is really helpful.
Hi,
Could someone please explain it to me the reason for the char assignment behavior?

public class Test {
public void take (char c){
System.out.println(c);
}
public static void main(String[] args) {
new Test().take(200); /// Compilation fails
}

}

class Test2 {
public static void main(String[] args) {
char c = 200;
System.out.println(c);
}
}

Why does compilation fails in Test class and the int assignment succeeds in the Test2 class?
Thanks Ben. Actually, the java 6 documentation is not very clear as java 7 documentation. It seems that there's no change in the join implementation as I removed the notify() call, the implicit notifyall() gets called.
http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#join%28%29

Also, I found an old post on this very same issue.
https://coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-code

Thanks again Ben.
Hi Everyone,
My first post on coderanch.

The problem which I am facing is : When notify() gets called in the below code, all the threads which are waiting gets notified and all starts execution. I am sure this could be a very basic problem but I just couldn't get it. Tried searching on the web, but found no answers unfortunately. Please help me.




Output:
Waiting - Thread-1
Waiting - Thread-2
Sleeping : Thread[Thread-0,5,main]
Notified Thread-1
Notified Thread-2