1

If I have a Vector of generics :Vector<E> vect in Java and I want to return a copy of the element in the position 'i' how can I do? If I am not wrong I think that when I use vect.get(i) it will return a reference to the object in the in the position i instead of a copy of it.

PS: I know nothing about the type and the objects the vector contains (for example, if they have the method clone, etc.).

  • 1
    Use `get`, then make a copy yourself with whatever process exists for that type. – Sotirios Delimanolis Dec 01 '15 at 16:41
  • 1
    Java lacks a generic way to copy objects. The only way is to query the type using `instanceof` or `getClass` then use a copy mechanism specific to the type. – Paul Boddington Dec 01 '15 at 16:45
  • see this: http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java – guillaume girod-vitouchkina Dec 01 '15 at 16:48
  • check with apache common bean utils it might have some thing what you are looking for; just a thought – JAVAC Dec 01 '15 at 16:58
  • 1
    `.get()` *does* return a copy of the element. You are confused about what the element is. The element is a reference. The only values in Java are references and primitives (as the only types are reference types and primitive types). All collections in the Java Collections Framework are collections of references. It does return a copy of the reference. – newacct Dec 03 '15 at 04:47

2 Answers2

0

There are different types of copying, which you can read in detail here and in your case if you E implements Cloneable interface then you can do as follow:

Let's assume your E is Employee

Employee employee = new Employee();
Employee noneReferencedCopyOfEmployee= (Employee) employee.clone(); 
Community
  • 1
  • 1
Raf
  • 7,505
  • 1
  • 42
  • 59
  • 1
    That's not generic though. You have to know the type of the elements. – Paul Boddington Dec 01 '15 at 16:52
  • @PaulBoddington you are right, what about applying your comment to find out type first? Would that work? My Generics and Collections book is on its way, I can't wait to discover more. – Raf Dec 01 '15 at 16:55
  • It would work, yes, but you'd have to know in advance all the possible types of objects that the lists could contain. – Paul Boddington Dec 01 '15 at 16:56
  • I understand you perfectly now, make sense – Raf Dec 01 '15 at 16:58
  • whether it implements `Cloneable` or not is irrelevant – newacct Dec 03 '15 at 01:18
  • @Raf: because how is it relevant? Whether something implements `Cloneable` is independent of whether it offers a `clone()` method. – newacct Dec 03 '15 at 04:45
  • @GerardoZinno I didn't say it is irrelevant, that is **newacct**'s comment. I need to read up more on generics. – Raf Dec 04 '15 at 21:35
  • @Raf sorry. Wrong tag. –  Dec 04 '15 at 21:38
  • @newacct it is not irrelevant to me at all. Why waste the time writing and implementing interfaces then? –  Dec 04 '15 at 21:39
  • @GerardoZinno: If you read the documentation, you would know that the `Cloneable` interface is only used as a marker interface to indicate to the `Object.clone()` implementation whether to throw an exception at runtime or not. It is only relevant to the internal implementations of clone methods in the class (which may or may not use `Object.clone()`) and is not relevant to outside code. – newacct Dec 04 '15 at 23:49
-1

If E does not implement the Cloneable<E> interface you can only return a reference to the object in the Vector. Otherwise just use the method clone().

  • There is nothing called `Cloneable`. And whether something implements the `Cloneable` interface is independent of whether it offers you a `clone()` method. – newacct Dec 03 '15 at 01:18