java吧 关注:1,249,072贴子:12,730,416
  • 15回复贴,共1

求助贴 求大神帮下忙 本人新人

只看楼主收藏回复

public static void main(String[] args) {
int a[] = { 23, 3, 33, 44, 56, 76872, 34, 45, 65, 6, 435, 789 };
int n = (int) (Math.random() * a.length);
随机出n然后将前面的n位数移到最后面 求大神帮忙


1楼2015-01-16 20:49回复
    自顶 来大神 谢谢了


    2楼2015-01-16 20:52
    回复
      不要沉啊 大婶们 快来啊


      3楼2015-01-16 20:54
      回复
        大神呢


        来自iPhone客户端4楼2015-01-16 21:03
        回复
          就是将生成的数放在第一位啦


          IP属地:湖北来自Android客户端5楼2015-01-16 21:05
          收起回复
            扩容,复制


            IP属地:湖北来自Android客户端6楼2015-01-16 21:06
            回复
              没有人给我说一下吗 大婶们


              7楼2015-01-16 21:44
              回复
                啥意思,n为5就把数组前5移到最后?


                IP属地:安徽来自Android客户端8楼2015-01-16 22:00
                收起回复
                  逆转两下


                  来自Android客户端10楼2015-01-16 22:57
                  收起回复
                    用数组复制就可以得到:
                    public class TestShuZu{
                    public static void main(String[] args) {
                    int[] a = { 23, 3, 33, 44, 56, 76872, 34, 45, 65, 6, 435, 789 };
                    int n =(int)(Math.random() * a.length);
                    int m=a.length-n;
                    System.out.println("随机N的位数:"+n);
                    int[] b =new int[n];
                    System.arraycopy(a,0,b,0,n);
                    int[] c=new int[m];
                    System.arraycopy(a,n,c,0,m);
                    int[] d=new int[a.length];
                    System.arraycopy(c,0,d,0,m);
                    System.arraycopy(b,0,d,m,n);
                    System.out.println("d数组:");
                    for(int i=0; i<d.length;i++){
                    System.out.print(d[i]+" ");
                    }
                    }
                    }


                    11楼2015-01-19 23:28
                    回复
                      public static void main(String args[]) {
                      int a[] = { 23, 3, 33, 44, 56, 76872, 34, 45, 65, 6, 435, 789 };
                      int n = new java.util.Random().nextInt(a.length);
                      System.out.println("The random >> " + n);
                      int newArr[] = move(a, n);
                      for(int i : newArr) {
                      System.out.print(i + " ");
                      }
                      }
                      private static int[] move(int a[], int n) {
                      int len = a.length;
                      if( n == 0 || n == len ) return a;
                      int newArr[] = new int[len];
                      for(int i = 0; i < len; i ++) {
                      newArr[i] = a[(n+i)%len];
                      }
                      return newArr;
                      }


                      IP属地:重庆12楼2015-01-20 00:05
                      回复
                        用数组拷贝:
                        public class TestShuZu{
                        public static void main(String[] args) {
                        int[] a = { 23, 3, 33, 44, 56, 76872, 34, 45, 65, 6, 435, 789 };
                        int n =(int)(Math.random() * a.length);
                        int m=a.length-n;
                        System.out.println("随机N的位数:"+n);
                        int[] b=new int[a.length];
                        System.arraycopy(a,n,b,0,m);
                        System.arraycopy(a,0,b,m,n);
                        System.out.println("b数组:");
                        for(int i=0; i<b.length;i++){
                        System.out.print(b[i]+" ");
                        }
                        }
                        }


                        13楼2015-01-20 09:22
                        回复