| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2128 人关注过本帖
标题:关于Linkedlist<E>泛型类
只看楼主 加入收藏
子良
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2010-4-22
收藏
得分:0 
更正一下:
程序代码:
import java.util.*;

public class Example_3{
    public static void main(String agrs[]){
    int block[ ][ ]=new int[4][4];
        LinkedList list=new LinkedList();
        for(int i=0;i<16;i++){
            if(i<=7)
                list.add(i+1);      
            else
                list.add(i-7);
        }
        System.out.println("输出链表:");
        for(int i=0;i<16;i++)
            System.out.println("第"+(i+1)+"个节点的值:"+list.get(i));
       
        //然后随机删除链表中的节点,同时将该节点中的数据顺序地放入方阵中
        for(int i=0;i<16;i++){
            int temp = (Integer) list.remove();
            for(int j=0;j<4;j++){
                for(int z=0;z<4;z++){
                    block[j][z]= temp;
                }
            }
        }
        for(int j=0;j<4;j++) {
               for(int z=0;z<4;z++) {
                   System.out.printf("%2s",block[j][z]);
               }
               System.out.printf("%n");
            }
    }
}
运行结果:
输出链表:
第1个节点的值:1
第2个节点的值:2
第3个节点的值:3
第4个节点的值:4
第5个节点的值:5
第6个节点的值:6
第7个节点的值:7
第8个节点的值:8
第9个节点的值:1
第10个节点的值:2
第11个节点的值:3
第12个节点的值:4
第13个节点的值:5
第14个节点的值:6
第15个节点的值:7
第16个节点的值:8
 8 8 8 8
 8 8 8 8
 8 8 8 8
 8 8 8 8

为什么后面全是8。谁能改下?
2010-05-10 20:33
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
程序代码:
import java.util.*;

public class Example_3 {
    public static void main(String args[]) {
        int block[][] = new int[4][4];
        LinkedList list = new LinkedList(); // 创建list
        for (int i = 0; i < 8; i++) {
            list.add(i + 1);
        }
        for (int i = 1; i <= 8; i++) {
            list.addLast(i);
        }
        for (int i = 0; i < 16; i++) {
            System.out.println("第" + (i + 1) + "节点中的数据:" + list.get(i));
        }

        int k = 16;
        Random random = new Random();
        for (int i = 0; i < 16; i++) {
            int tempIndex = random.nextInt(k);
            int temp = (Integer) list.remove(tempIndex);
            k--;
            block[i / 4][i % 4] = temp;
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.printf("%2d", block[i][j]);
            }
            System.out.printf("%n");
        }
    }
}

楼主,我第一个发帖就已经告诉你答案了呀。。。。
2010-05-10 21:45
子良
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2010-4-22
收藏
得分:0 
回复 12楼 linjx0123
我不得不承认你这种方法更高,我们老师只要我们用block[4][4]=(Integer)list.remove();谢谢了。
我的代码公布一下:
程序代码:
import java.util.*;

public class Example_3{
    public static void main(String agrs[]){
    int block[ ][ ]=new int[4][4];
        LinkedList list=new LinkedList();
        for(int i=0;i<16;i++){
            if(i<=7)
                list.add(i+1);      
            else
                list.add(i-7);
        }
        System.out.println("输出链表:");
        for(int i=0;i<16;i++)
            System.out.println("第"+(i+1)+"个节点的值:"+list.get(i));
       
        //然后随机删除链表中的节点,同时将该节点中的数据顺序地放入方阵中
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                block[i][j]=(Integer) list.remove();
            }
        }
        /*for(int i=0;i<16;i++){
            int temp = (Integer) list.remove();
            for(int j=0;j<4;j++){
                for(int z=0;z<4;z++){
                    block[j][z]= temp;
                }
            }
        }*/
        /*Iterator iter=list.iterator();
        while(iter.hasNext()){
            int te=(Integer)iter.next();
            System.out.println(te);
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    block[i][j]= te;
                }
            }
        }*/
        for(int i=0;i<4;i++) {
               for(int j=0;j<4;j++) {
                   System.out.printf("%2s",block[i][j]);
               }
               System.out.printf("%n");
            }
    }
}

 
2010-05-11 13:05
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
楼主,你最后的代码我看了,但是你并不能完成题目的要求,因为你只是按顺序放进矩阵中。
要注意,题目要求是将整数1~8随机放入方阵中。是随机。也就是每一次运行,结果都可能是不一样的。
假如要顺序放进去,根本不需要建立一个链表。代码如下:
程序代码:
public class Example_3 {
    public static void main(String args[]) {
        int block[][] = new int[4][4];
        int k = 1;
       
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if(k>8)
                    k = 1;
                block[i][j] = k;
                k++;
            }
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.printf("%2s", block[i][j]);
            }
            System.out.printf("%n");
        }

    }
}


2010-05-11 14:48
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
收藏
得分:0 
回复 9楼 linjx0123
刚才查看下doc,
public int nextInt(int n)Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability. The method nextInt(int n) is implemented by class Random as follows:
 public int nextInt(int n) {
     if (n<=0)
                throw new IllegalArgumentException("n must be positive");

     if ((n & -n) == n)  // i.e., n is a power of 2
         return (int)((n * (long)next(31)) >> 31);

     int bits, val;
     do {
         bits = next(31);
         val = bits % n;
     } while(bits - val + (n-1) < 0);
     return val;
}
 The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm shown would choose int values from the stated range with perfect uniformity.

The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2.

The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two.


Parameters:
n - the bound on the random number to be returned. Must be positive.
Returns:
a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
Throws:
IllegalArgumentException - n is not positive.
Since:
1.2
应该是不会有负数了。好久不看了,有点忘记了,抱歉。讨论总是可以唤醒曾经的激情,谢谢你。
2010-05-11 23:38
wtuaimmmm
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-19 21:46
wtuaimmmm
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-21 20:39
快速回复:关于Linkedlist<E>泛型类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027277 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved