Java练习十题集(二)java
1.编程输出如下格式的数据。算法
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.编程输出如下格式的数据。cookie
when i=1:网络
1
4 5 2
3 session
when i=2:
1 2
8 9 10 3
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.编程输出如下格式的数据。
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:
1
4 5 2
3
when i=2:
1 2
8 9 10 3
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。