This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Review question #24 of chapter 3 (Java OCA 8 Programmer I Study Guide)  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer on page 342 says that

24. C. Converting from an array to an ArrayList uses Arrays.asList(names). There is
no asList() method on an array instance. If this code were corrected to compile, the
answer would be option A.



the question is as followingA. Sue
B. Tom
C. Compiler error on line 7.
D. Compiler error on line 8.
E. An exception is thrown.

But in the book it says that if we convert array to list then the list will be immutable. So if we correct code ( line 7 instead of names.asList() to Array.asList())  then I assume it should throw an exception in runtime.
 
Greenhorn
Posts: 20
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A List generated by a conversion from an Array must follow the same rules that Array objects do follow. And that's because the List is linked to the Array from which got generated (a change to the List reflects on the Array).
So, if you have an Array of a certain size, can you change that size? No, you can't. But can you change the value of an existing element? Of course you can.
Same as for the linked List: you can't add elements, but you can change the existing ones.
 
author & internet detective
Posts: 39148
724
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. It's not that it is immutable. It is that it is fixed size. Also known as a "backed array".
 
Etibar Hasanov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran the following code from the example

But I wonder when it tries to set "Sue", it gives ArrayIndexOutOfBoundsException exception
 
Marshal
Posts: 6577
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Etibar Hasanov wrote:But I wonder when it tries to set "Sue"

At this point List is size zero probably. Try to use add() instead set().
 
Etibar Hasanov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Etibar Hasanov wrote:But I wonder when it tries to set "Sue"

At this point List is size zero probably. Try to use add() instead set().



add doesn't work, and in my opinion it shouldn't work as well. Because the list size gets fixed  if it is converted from Array into List
 
Liutauras Vilda
Marshal
Posts: 6577
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what exactly you're trying. In both versions of your code you were using something illegal. In top version you used non existing method, in bottom something else incorrectly. Check mine version if it is what you want.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I remember that Java allow to call a static method on an instance. Why does names.asList cause a compile error?
 
Marshal
Posts: 63314
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does an array have an asList() method?
 
Micheal Bush
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Does an array have an asList() method?



Thanks, I kinda need a concrete answer. Do you have any readings that I can rely on ?
 
Jeanne Boyarsky
author & internet detective
Posts: 39148
724
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because "names" isn't an instance of the Arrays class. Arrays are special.

Try running this code:


Notice how it doesn't print Arrays.class?
 
Micheal Bush
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Because "names" isn't an instance of the Arrays class. Arrays are special.

Try running this code:


Notice how it doesn't print Arrays.class?




It outputs class [Ljava.lang.String;

So array's instance is base on the declaration type? Can an Array class has an instance of itself ?
 
Marshal
Posts: 24184
54
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Micheal Bush wrote:So array's instance is base on the declaration type? Can an Array class has an instance of itself ?



Of course. An array of String is not the same as an array of JTable, or an array of any other object type either. An array of type T can only contain objects of type T (or more accurately, references to objects of type T).

And yes, there are cases where an Array object can contain a reference to itself. But an array of String objects can't contain a reference to itself, because an array is not a String. Likewise an array of JTable objects can't contain a reference to itself, because an array is not a JTable. In fact there's only one type T for which an array of T objects can contain a reference to itself... can you work out what type that is?
 
Micheal Bush
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Micheal Bush wrote:So array's instance is base on the declaration type? Can an Array class has an instance of itself ?



Of course. An array of String is not the same as an array of JTable, or an array of any other object type either. An array of type T can only contain objects of type T (or more accurately, references to objects of type T).

And yes, there are cases where an Array object can contain a reference to itself. But an array of String objects can't contain a reference to itself, because an array is not a String. Likewise an array of JTable objects can't contain a reference to itself, because an array is not a JTable. In fact there's only one type T for which an array of T objects can contain a reference to itself... can you work out what type that is?



Thanks. The first thing come to my mind is an array holds another array (like 2D array) but the inner array could be any types as well. So I guess I don't know the answer to your question. And Is array the only one class which you are not able to call a static method from its instance syntactically?
 
Campbell Ritchie
Marshal
Posts: 63314
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Micheal Bush wrote:. . . The first thing come to my mind is an array holds another array (like 2D array) . . .

No, becuase the inner arrays are a different type from their surrounding array. That is not a 2D array, but an array of arrays.

And Is array the only one class which you are not able to call a static method from its instance syntactically?

No. That is only because arrays don't have any static methods. It is always bad programming to call a static method on an object name; under some cirucmstances it is possible to call a method not declared in the class that object was created from.

Hint for the question about arrays containing themselves: what is the superclass of an array? The question wants the following code both to compile and execute normally:-
 
Micheal Bush
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Micheal Bush wrote:. . . The first thing come to my mind is an array holds another array (like 2D array) . . .

No, becuase the inner arrays are a different type from their surrounding array. That is not a 2D array, but an array of arrays.

And Is array the only one class which you are not able to call a static method from its instance syntactically?

No. That is only because arrays don't have any static methods. It is always bad programming to call a static method on an object name; under some cirucmstances it is possible to call a method not declared in the class that object was created from.

Hint for the question about arrays containing themselves: what is the superclass of an array? The question wants the following code both to compile and execute normally:-




Object? Thats two line of code looks pretty confusing to me. Why do you assign array back to its elements?
 
Paul Clapham
Marshal
Posts: 24184
54
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Micheal Bush wrote:Object?


Yes, that's the right answer. Congratulations!

Thats two line of code looks pretty confusing to me. Why do you assign array back to its elements?


Hey! Remember we're in a forum about certification exams here! All kinds of ridiculous code examples are fair game in those exams. Almost none of the examples are things which programmers would do in real life, they are just there to test your ability to interpret Java code.

So...



This will compile, and after it runs the array will contain a reference to itself. I suppose there might actually be somebody who can make use of that "feature", I wouldn't rule that out. But don't ask me why they would want to do that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!