登录 | 首页 -> 华新鲜事 -> 技术の宅 | 切换到:传统版 / sForum | 树形列表
[java] how to cast after clone()?
<<始页  [1]  末页>> 

[java] how to cast after clone()?
Vector<A> a= new Vector<A>();
Vector<A> b= a.clone(); //1.
Vector<A> b=(Vector) a.clone();//2.
Vector<A> b=(Vector<A>) a. clone(); //3

i have tried all the three ways of casting, but all of them will produce either an error or a warning. Can anyone suggest how to do casting in this case? thanks a lot in advance!

[蚂蚁 (4-6 2:18, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

what is Vector<A> for?Vector[]?[逃课专家 (4-6 9:23, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

我试了,第三个可以啊[atp@gateway ~]$ cat aaa.java
import java.util.Vector;

public class aaa
{
  public static void main (String [] s)
  {
    Vector<String> a = new Vector<String>();
    a.add("hi");
    Vector<String> b = (Vector<String>)a.clone();
    System.out.println(b);
  }
}
[atp@gateway ~]$ javac aaa.java
Note: aaa.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[atp@gateway ~]$ java aaa
[hi]
[atp@gateway ~]$ 
[吴永铮 (4-6 12:50, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

try Vector<String> b = new Vector<String>(a);try this

Vector<String> b = new Vector<String>(a);
[绿竹幽径 (4-6 19:07, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

(引用 绿竹幽径:try Vector b = new Vector(a);try this Vector b = new Vector(a);...)有一点点不一样Vector<String> b = new Vector<String>(a); // this is shallow clone
Vector<String> b = (Vector<String>)a.clone(); // this is deep clone
[吴永铮 (4-6 23:59, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼

(引用 吴永铮:我试了,第三个可以啊[atp@gateway ~]$ cat aaa.java import java.util.Vector; public class aaa {   public static void main (Stri...)多谢!第三个是可以,不过还是有
Note: aaa.java uses unchecked or unsafe operations
难道就没有办法可以避免吗?
[蚂蚁 (4-7 4:20, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼

(引用 逃课专家:what is Vector for?Vector[]?...)就是说这个vector里面放的都是A这个class的object[蚂蚁 (4-7 4:21, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼

(引用 吴永铮:有一点点不一样Vector b = new Vector(a); // this is shallow clone Vector b = (Vector)a.clone(); // this is deep clone...)能解释一下deep clone and shallow clone 么?[icky (4-7 9:04, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼

(引用 icky:能解释一下deep clone and shallow clone 么?)Shallow cloneShallow clone: if original object's value is changed, and the copy of that object also changes.
[抽烟看球 (4-7 12:21, Long long ago)] [ 传统版 | sForum ][登录后回复]9楼

(引用 蚂蚁:多谢!第三个是可以,不过还是有 Note: aaa.java uses unchecked or unsafe operations 难道就没有办法可以避免吗?)Leave itthis warning message just shows that compiler cannot assure correctness of this casting operation as clone() method returns type of object.

It is really programmer's responsibility to avoid any casting failure.
[抽烟看球 (4-7 14:09, Long long ago)] [ 传统版 | sForum ][登录后回复]10楼

(引用 抽烟看球:Shallow cloneShallow clone: if original object's value is changed, and the copy of that object also changes. )is it similar to copy pointer and copy objects in C?[icky (4-7 18:56, Long long ago)] [ 传统版 | sForum ][登录后回复]11楼

(引用 抽烟看球:Leave itthis warning message just shows that compiler cannot assure correctness of this casting operation as clone() method retu...)总是觉得compile之后还有一行字,看起来实在是很不爽![蚂蚁 (4-8 3:10, Long long ago)] [ 传统版 | sForum ][登录后回复]12楼

(引用 icky:is it similar to copy pointer and copy objects in C?)I didnt make my statement clear enufin this case, a shallow clone will make an extra copy of Vector object. ( only container object is copied)

however, a deep clone will create memory for Vector object and all String objects inside this Vector. ( container and contained objects are all duplicated)

[抽烟看球 (4-9 15:52, Long long ago)] [ 传统版 | sForum ][登录后回复]13楼

(引用 抽烟看球:I didnt make my statement clear enufin this case, a shallow clone will make an extra copy of Vector object. ( only container obj...)so shallow is just assign a reference , am I right[门中木 (4-10 22:00, Long long ago)] [ 传统版 | sForum ][登录后回复]14楼

(引用 门中木:so shallow is just assign a reference , am I right)copy object for container, copy referrence for contained object[抽烟看球 (4-10 23:27, Long long ago)] [ 传统版 | sForum ][登录后回复]15楼

1.5的东东比1.4变了好多,搞的连java也不会写了^_^[门中木 (4-13 0:04, Long long ago)] [ 传统版 | sForum ][登录后回复]16楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 技术の宅 | [刷新本页] | 切换到:传统版 / sForum