Java实验2nd:数组&字符串

实验写的小程序懒得放到电脑上占空间了,就放到这里吧。

目录

一、统计随机数... 1

二、连接字符串... 2

三、统计字符串... 3

四、随机字符... 5

一、统计随机数

public classRandomNum {

/**

* 产生的随机数的次数。默认为10000次。

*/

private static final int COUNT = 10000;

/**

* 产生的随机数的范围。默认为[0,20]

*/

private static final int NUM_RANGE = 20;

public static void main(String[] args) {

int[] cntArr =new int[NUM_RANGE + 1];// 全自动初始化为0

int num;

for (int i = 0; i <COUNT; ++i) {

num= (int)(Math.random() *NUM_RANGE + 0.5); // 四舍五入,NUM_RANGE会取到

++cntArr[num];

}

System.out.println("\t个数\t百分比\t差值");

System.out.printf("标准:\t%.0f\t%.2f%%\t0\n", (double)COUNT

/(NUM_RANGE+ 1), 100.0 / (NUM_RANGE + 1));

int cnt;

double rate;

for (int i = 0; i < cntArr.length; ++i) {

cnt= cntArr[i];

rate= cnt * 100.0 / COUNT;

System.out.print(i +"\t" + cnt +"\t"+ rate + "%");

System.out.printf("\t%.2f%%\n",

Math.abs(rate- 100.0 / (NUM_RANGE+ 1)));

}

}

}

运行结果:

个数 百分比 差值

标准: 476 4.76% 0

0: 272 2.72% 2.04%

1: 503 5.03% 0.27%

2: 521 5.21% 0.45%

3: 475 4.75% 0.01%

4: 508 5.08% 0.32%

5: 513 5.13% 0.37%

6: 518 5.18% 0.42%

7: 502 5.02% 0.26%

8: 540 5.4% 0.64%

9: 479 4.79% 0.03%

10: 486 4.86% 0.10%

11: 516 5.16% 0.40%

12: 454 4.54% 0.22%

13: 485 4.85% 0.09%

14: 479 4.79% 0.03%

15: 488 4.88% 0.12%

16: 509 5.09% 0.33%

17: 541 5.41% 0.65%

18: 496 4.96% 0.20%

19: 471 4.71% 0.05%

20: 244 2.44% 2.32%

二、连接字符串

public classIntArrayStr {

/**

* 把一个整数数组中的每个元素用逗号连接成一个字符串

*

* @param arr

*要转换的整数数组

* @return转换后的字符串

*/

public static String convert(int[] arr) {

Stringstr = "";

for (int i = 0; i < arr.length; ++i) {

str+= arr[i];

if (i != arr.length - 1) {

str+= ",";

}

}

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] array = { 1, 2, 3, 4,5, 6 };

StringintStr = convert(array);

System.out.println(intStr);

}

}

运行结果:

1,2,3,4,5,6

三、统计字符串

public classStrStr {

/**

* 统计在字符串数组arr中以str开头的字符串有多少个。

*

* @param arr

*字符串数组

* @param str

*要匹配的子串

* @return返回匹配成功的字符串个数

*/

public static int countHead(String[] arr,String str) {

int cnt = 0;

for (String sElem : arr) {

if (0 ==sElem.indexOf(str)) {// 首次匹配的索引为0即在开头匹配成功。

++cnt;

}

}

return cnt;

}

/**

* 统计在字符串数组arr中以str结尾的字符串有多少个。

*

* @param arr

*字符串数组

* @param str

*要匹配的子串

* @return返回匹配成功的字符串个数

*/

public static int countTail(String[] arr,String str) {

int cnt = 0;

for (String sElem : arr) {

// 从后向前首次匹配的索引为大串与小串之差即在结尾。为防止是-1,须保证大串一定不小于小串。

if (sElem.length() >=str.length()

&&sElem.length() - str.length() == sElem.lastIndexOf(str)) {

++cnt;

}

}

return cnt;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String[]s = { "string","starting","strong","street","stir",

"studeng","soft","sting"};

StringstrHead = "st";

StringstrTail = "ng";

int numHead =countHead(s,strHead);

int numTail =countTail(s,strTail);

System.out.println("在字符串数组:");

for (String sElem : s) {

System.out.println(sElem);

}

System.out.println("\"" + strHead +"\"为开头的字符串有" + numHead +"个。");

System.out.println("\"" + strTail +"\"为结尾的字符串有" + numTail +"个。");

}

}

运行结果:

在字符串数组:

string

starting

strong

street

stir

studeng

soft

sting

以"st"为开头的字符串有7个。

以"ng"为结尾的字符串有5个。

四、随机字符

public classRandomChar {

private static final int CHAR_NUM = 10;

private static final int LETTER_NUM = 26;

public static void main(String[] args) {

// 用空间换时间,arrChar[]保存产生的字母,arr[]作计数排序

int[] arrChar =new int[CHAR_NUM];

boolean[] arr =new boolean[LETTER_NUM];

for (int i = 0; i <CHAR_NUM;) {

int letter = (int) (Math.random()* (LETTER_NUM- 1) + 0.5);// [0,25]

if (!arr[letter]) {

arrChar[i]= letter;

arr[letter]= true;

++i;

}

}

System.out.println("排序前:");

for (int c : arrChar) {

System.out.print((char) ('a' + c) +" ");

}

System.out.println("\n排序后:");

for (int i = 0; i < arr.length; ++i) {

if (arr[i]) {

System.out.print((char) ('a' + i) +" ");

}

}

}

}

运行结果:

排序前:

w p s a e o f n y t

排序后:

a e f n o p s t w y



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值