Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning immutable reference  RSS feed

 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

Help me please. I'm stuck with my hometask from Algorithms course at coursera. I've got next code:

 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to say: we can't use Arrays.copyOf() method either, because MyClass doesn't contain it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7927
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So then, how would you copy an array without using any methods to do it for you?
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bot who checks our hometasks and here is the output:

M V EI_EXPOSE_REP EI: Returns a reference to the mutable object stored in the instance variable 'myArray', which exposes the internal representation of the class 'Test'. Instead, create a defensive copy of the object referenced by 'myArray' and return the copy.

Test 10: Check that data type is immutable by testing whether each method
        returns the same value, regardless of any intervening operations
  * input8.txt
    - failed after 4 operations involving Test class
    - first and last call to getArray() returned different arrays
    - sequence of operations was:
          Test collinear = new Test()
          collinear.getArray()
          mutate array returned by last call to segments()
          collinear.segments()
    - failed on trial 1 of 100
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyClass[] myArray is a coordinate points array (x,y), and it's possible to put (x,y) there, but impossible (at least for me) to get them from this class. It has only constructor, but not getters or setters.
So, actually, I just don't know how to duplicate MyClass variable without refferencing it.
 
Paul Clapham
Sheriff
Posts: 22681
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksey Movchan wrote:MyClass[] myArray is a coordinate points array (x,y), and it's possible to put (x,y) there, but impossible (at least for me) to get them from this class. It has only constructor, but not getters or setters.
So, actually, I just don't know how to duplicate MyClass variable without refferencing it.


Originally you seemed to understand that you need to return a copy of the array in the Test class, rather than the array itself. But now you've gone off into the swamp. I can't make any sense of much of what you just posted, except the part where you think you have to "duplicate a MyClass variable". Let's change that to duplicating a MyClass object, since duplicating an variable is trivial, and then my response is that you don't need to duplicate any MyClass objects.
 
Stephan van Hulst
Saloon Keeper
Posts: 7927
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't modify objects of MyClass, then you don't have to worry it either, right? All you need to do, is copy an array without calling any methods to do it.
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you suggesting one shou‍ld find some way for an array to copy itself?
 
Stephan van Hulst
Saloon Keeper
Posts: 7927
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksey Movchan wrote:
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you will have to write an array copying routine yourself. Nothing for it. I think that is a daft requirement, but you will have to comply with it if you want good marks.
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:If you can't modify objects of MyClass, then you don't have to worry it either, right? All you need to do, is copy an array without calling any methods to do it.


The program that checks my homework automatically, can somehow change MyClass[] refference. I understand it from this output (I copied a bit wrong output last time, sorry):
====================
Test 10: Check that data type is immutable by testing whether each method
        returns the same value, regardless of any intervening operations:
        - sequence of operations was:
          Test collinear = new Test()
          collinear.getArray()
          mutate array returned by last call to getArray()
          collinear.getArray()
      - failed!
====================

As I can see, in the main method program gets the refference from getArray() on private final MyClass[] myArray, and somehow changes one of the elements of that array, or adding additional element, I don't know. And calls getArray() again after that. And tells us that results of the first call and second call are different, but must be the same.

There is a thread at Coursera where mentor says next:
The key point is "If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.".
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
The key phrase is "create copies of your internal mutable objects when necessary to avoid returning the originals in your methods."


But I just don't realise how to do that without Arrays.copyOf() or clone().
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you wonder how MyClass looks like:



the goal is to create a copy of MyClass[].
We can't add new methods to it.
May be they wan't us to split returned String, but I'm not sure that it's about that. I hope there are another methods...
 
Junilu Lacar
Sheriff
Posts: 11428
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are overthinking this. You learned about arrays. You probably learned how to declare/create a new array. So do what you've been taught and declared/create a new array that can hold as many elements as the old array! Then copy all the values that are in the old array into the new array. That's it.
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all guys, it was just my stupidity.
Actually, I can implement next code in my hometask:



The last part of hometask looked like this:
You may not call any library functions other those in java.lang, java.util, and algs4.jar. In particular, you may call Arrays.sort().


So I thought it wouldn't be acceptable to use Arrays.copyOf(), but it worked. Got 98/100
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!