Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Tue Oct 13, 2015 10:50 am
by klhlubek
I think I have found a little ( It is very little) error. Though many people make that I would say it is clearly an error, so I would please you to correct it. Here is what I mean:
Syntactically, this lambda expression is correct. However, remember that a lambda expression does not create a new scope for variables. Therefore, you cannot reuse the variable names that have already been used to define new variables in your argument list .
Here, observe that the variable d is already defined so your argument list cannot use d as a variable name. It would be like defining the same variable twice in the same scope.
That is the first explanation of the first question and there is written about an argument list ( it is bold in the quoted test ), but what actually is meant is parameter list, because:

An argument list is a list of arguments, when you define for example a method.
A parameter list is a list of paramters and you call with a parameter list something like for example a method, but it has to fit with the argument list of the calles method.

So there is a difference between argument and parameter. ( This difference is for me very important, but I do not know how for you ).


There is a another thing about it. I would say it could be seen as correct, because you "define" the argument list for the method, which is later called ( it is a lambda expression ), but I would like it if you would add something to your explanation to make it more clear.

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Wed Oct 14, 2015 9:36 pm
by admin
Yes, there is a difference between an argument and a parameter as you highlighted.
This explanation, however, is talking from a perspective of the call

Code: Select all

filterData(al, d -> d.value%2 == 0 );
.
Here, al and the lambda expression are actually arguments to the filter data method and not parameters.

I agree with your point in general, unfortunately, if you use the word parameter here, it will be confusing from the other perspective. Lambda expressions are peculiar in this respect because there is no clear distinction between an argument and a parameter. You actually specify arguments but the compiler creates a method definition out of it with parameters.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Fri Apr 27, 2018 1:40 am
by Lunarkiran

Code: Select all

 public class Data {
	int value;

	Data(int value) {
		this.value = value;
	}

	public String toString() {
		return "" + value;
	}

	

	public static void main(String[] args) {

	ArrayList<Data> al=new ArrayList<>();
Data d = new Data(1);
al.add(d);
d=new Data(2);al.add(d);d=new Data(3);al.add(d);

filterData(al, x -> x.value%2 == 0 );
System.out.println(al);
}
	public static void filterData(ArrayList<Data> dataList, Predicate<Data> p) {
		Iterator<Data> i = dataList.iterator();
		while (i.hasNext()) {
			if (p.test(i.next())) {
				i.remove();
			}
		}
}
}

but option A code snipet working fine why itis wrong.

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Fri Apr 27, 2018 2:05 am
by admin
Make sure you type the code exactly as given in the question.

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Mon Jun 01, 2020 2:50 pm
by FanaticuS
Hi,

Shouldn't filterData(ArrayList<Data> dataList, Predicate<Data> p) method be static for the method call(from option B) to compile? Or am I missing something?

Regards,
A

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Tue Jun 02, 2020 2:20 am
by admin
Not necessarily. The code fragment could exist in an instance method of the same class in which the filterData method exists.

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Sun Mar 21, 2021 5:00 am
by f.motta
It didn't should have the "ArrayList<Data> al = new ArrayList<>()" declaration ? Or it is implicitly in the hidden "...." code?

And we must know Iterator for the exam?

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Posted: Sun Mar 21, 2021 8:00 am
by admin
I see that ArrayList<Data> al = new ArrayList<Data>(); is there in the code.
Although Iterator is not explicitly mentioned in OCAJP 8 but it is good to know because iterating through a list using the for-each loop is on the exam and for-each requires an Iterator.