Java练习十题集(二)

Java练习十题集(二)

1.编程输出以下格式的数据。

When i=0

1

When i=1

7   8   9
6   1   2
5   4   3

When i=2

21  22  23  24  25
20    7    8    9  10
19    6    1    2  11
18    5    4    3  12
17  16  15  14  13

2.编程输出以下格式的数据。

when i=1:

1
4   5   2
3

when i=2:

1   2
8   9  10   3

7  12 11  4
6   5 

3.将AB2C3D4ESF6G7H8拆分开来,并分别存入int[]和String[]数组。得到的结果为[1,2,3,4,5,6,7,8]和[A,B,C,D,E,F,G,H]。
4.使用随机算法产生一个数,要求把1-1000W之间这些数全部生成。
5.cookie与session的区别于联系。

 

 

 

 

 

 

 

1.编程输出以下格式的数据。

When i=0

1

When i=1

7   8   9
6   1   2
5   4   3

When i=2

21  22  23  24  25
20    7    8    9  10
19    6    1    2  11
18    5    4    3  12
17  16  15  14  13

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Test();
    }

   public static void Test(){
       System.out.print("when i=");
       Scanner scanner = new Scanner(System.in);
       int n = scanner.nextInt();
       int m = 2*n+1;
       int[][] arr = new int[m][m];
       int value =(int)Math.pow(m, 2);
       spiralArray(arr, value, m);
       printMatrix(arr);
   }

    /**
     * 输出螺旋矩阵
     */
   static int[][] spiralArray( int[][] arr, int value, int n) {
       int start, end, top, bottom;
       start = top = 0;
       end = bottom = n-1;
       while (start<=end||top<=bottom) {
           for (int i = bottom; i >= top; i--) {
               arr[top][i] = value--;
           }
           top++;
           for (int i = top; i <= bottom; i++) {
               arr[i][start] = value--;
           }
           start++;
           for (int i = start; i <= bottom; i++) {
               arr[end][i] = value--;
           }
           end--;
           for (int i = end; i >= top; i--) {
               arr[i][bottom] = value--;
           }
           bottom--;
       }
       return arr;
   }

    /**
     * 打印数组输出结果
     */
   static void printMatrix( int[][] arr) {
       int n = arr.length;
       System.out.println();
       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n; j++) {
               System.out.print( arr[i][j]+"\t");
           }
           System.out.println();
       }
   }

}

  

2.编程输出以下格式的数据。

when i=1:


4   5   2
3

when i=2:

1   2 
8   9  10   3

7  12 11  4
6   5 
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Test();
    }

   public static void Test(){
       System.out.print("when i=");
       Scanner scanner = new Scanner(System.in);
       int n = scanner.nextInt();
       int[][] arr = new int[n + 2][n + 2];
       int value = 1;
       arr = outerArray(arr, value, n);
       printMatrix(arr,n);
   }

    /**
     * 单独处理最外层
     */
   static int[][] outerArray(int[][] arr, int value, int n) {
       for (int i = 0; i < n; i++) {
           arr[0][i] = value++;
       }
       for(int i=1;i<=n;i++) {
           arr[i][n+1] = value++;
       }
       for (int i = n; i > 0; i--) {
           arr[n + 1][i-1] = value++;
       }
       for (int i = n; i > 0; i--) {
           arr[i][0] = value++;
       }
        //中间部分看作螺旋矩阵
       spiralArray(arr, value, n);
       return arr;
   }

    /**
     * 输出螺旋矩阵
     */
   static int[][] spiralArray( int[][] arr, int value, int n) {
       int start, end, top, bottom;
       start = top = 1;
       end = bottom = n;
       while (start<end||top<bottom) {
           for (int i = start; i <= end; i++) {
               arr[top][i] = value++;
           }
           top++;
           for (int i = top; i <= bottom; i++) {
               arr[i][end] = value++;
           }
           end--;
           for (int i = end; i >= start; i--) {
               arr[bottom][i] = value++;
           }
           bottom--;
           for (int i = bottom; i >= top; i--) {
               arr[i][start] = value++;
           }
           start++;
       }
       /**
        * 如果i是奇数要将中间一位单独处理,i为偶数则不需要
        */
       if (n % 2 == 1) {
           arr[(n+1)/2][(n+1)/2] = value;
       }
       return arr;
   }

    /**
     * 打印数组输出结果
     */
   static void printMatrix( int[][] arr,int n) {
       System.out.println( );
       for (int i = 0; i < arr[0].length - 2; i++) {
           System.out.print(arr[0][i] + "\t");
       }
       System.out.println( );
       for (int i = 1; i < arr.length-1 ; i++) {
           for (int j = 0; j < arr[0].length; j++) {
               System.out.print(arr[i][j] + "\t");
           }
           System.out.println( );
       }
       for (int i = 0; i < arr[0].length - 2; i++) {
           System.out.print(arr[n+1][i] + "\t");
       }
       System.out.println( );
   }

}

  

3.将AB2C3D4ESF6G7H8拆分开来,并分别存入int[]和String[]数组。得到的结果为[1,2,3,4,5,6,7,8]和[A,B,C,D,E,F,G,H]。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        strTest();
    }

    static void strTest(){
        String str = "AB2C3D4ESF6G7H8";
        List<Character> charList = new ArrayList<>();
        List<Integer> intList = new ArrayList<>();
        for(int i = 0; i < str.length(); i++) {
            char x = str.charAt(i);
            if(x - 'A' >= 0 && x - 'A' <= 26){
                charList.add(x);
            }else if(x - '0' >= 0 && x - '0' <= 10){
                intList.add(Integer.parseInt(x+""));
            }
        }
        int[] nums = new int[intList.size()];
        int i = 0, j = 0;
        for(Integer intObj : intList) {
            nums[i++] = intObj;
        }
        System.out.println(Arrays.toString(nums));
        String[] strs = new String[charList.size()];
        for(Character charObj : charList) {
            strs[j++] =charObj+"";
        }
        System.out.println(Arrays.toString(strs));
    }

}

 

4.使用随机算法产生一个数,要求把1-1000W之间这些数全部生成。(考察高效率,解决产生冲突的问题)

提高效率的地方有如下:

1.初始化set集合的时候 Sets.newHashSetWithExpectedSize(value),
给初始化带个固定大小,减少了集合在扩容的时候,值重新复制的问题。这的效率稍有提高。

2.Random random = new Random();放在循环之外。

import com.google.common.collect.Sets;
import java.util.Random;
import java.util.Set;

public class Test {

    public static void main(String[] args) {
        testRandom();
    }

    /**
     * 使用随机算法产生一个数,要求把1-1000W之间这些数全部生成。
     * (考察高效率,解决产生冲突的问题)
     */
    static void testRandom() {
        int value = 10000000;
        //int类型最大值:2的32次方 - 1 = Integer.MAX_VALUE = 2147483647,二十亿多,真够啦 。
        Set<Integer> result = Sets.newHashSetWithExpectedSize(value);
        Random random = new Random();
        long a = System.currentTimeMillis();
        while (result.size() < value + 1) {
            int i = random.nextInt(value + 1);
            result.add(i);
        }
        System.out.println("\r<br> 执行耗时 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 ");
        System.out.println("完了,集合大小为" + result.size());

    }

}

 

5.cookie与session的区别于联系。

cookie:
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密 )。Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下 次请求同一网站时就发送该Cookie给服务器(前提是浏览器设置为启用cookie)
session:
在计算机中,尤其是在网络应用中,称为“会话控制”。Session对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序 的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。
session和cookie的区别:
session是存储在服务器的,以文本的形势存储在硬盘。cookie是存储在客户端(浏览器),存储在内存中。当访问量过多时,会影响服务器的性能,一般使用代理服器存储session。cookie在浏览器可以修改,安全性低,session安全性高。单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。

 

 

 

 

 

转载于:https://www.cnblogs.com/loytime/p/10133039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值