int idx = r.nextInt(i);
  Card x = (Card)cards.get(i);
  x  = (Card)cards.set(idx, x);
      
 cards.set(i,x);
怎么理解后三句这个程序呢?java

解决方案 »

  1.   


      Card x = (Card)cards.get(i);//得到卡堆中第i位置的卡片,并赋值给x
       x  = (Card)cards.set(idx, x);//将x卡和卡堆中第idx位置的卡片对换,此时x就是cards中第idx位置的卡片
           
      cards.set(i,x);将此时的x插入第i位置//总之这是在把cards中第i位置和第idex位置的card对象进行交换~
      

  2.   

    //////为什么这一句是插入到地i位置,能不能理解为x和第i位置的卡片交换位置?
      

  3.   


    x是card。
    你要不看看doc上咋写的:
    public E set(int index,
                 E element)
    Replaces the element at the specified position in this list with the specified element. 再不行你拿段代码去测试看看 String a = new String("A");
    String b = new String("B");
    ArrayList<String> stringList = new ArrayList<String>();//这是stringList,存放着A和B
    stringList.add(a);
    stringList.add(b);

    System.out.println(stringList.toString());//输出结果是【A,B】

    String tmp = stringList.get(0);//tmp = "A"
    tmp = stringList.set(1, tmp);//tmp = “B”;
    stringList.set(0, tmp);
    System.out.println(stringList.toString());