java字符串数字统计_对字符串进行简单的字符数字统计 探索java中的List功能

题目:

统计一个字符串中数字和字符串的个数,并分别进行排列,要求

1.数字,字符串可以从键盘获取。

2.储存在list

3.统计数字个数,字符串个数

4.把数字和字符串按从小到大的顺序输出

5.不能使用数组.

List的用法

List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法。【自行百度】

List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象。

List接口的常用实现类有ArrayList和LinkedList,在使用List集合时,通常情况下声明为List类型,实例化时根据实际情况的需要,实例化为ArrayList或LinkedList,例如:

List l = new ArrayList();// 利用ArrayList类实例化List集合

但是!在笔者的eclipse中,如果是在main函数中申明的话,需要写全,不然会出现很美妙的红色波浪线【笔者在这里纠结了好久好久。。。。】

java.util.List list=new ArrayList();

但是在public class中就直接申明就好

1

2 static List number=new ArrayList();3 static List word=new ArrayList();4

这里是申明了两个string型的list,分别用来存放字符串中的数字和字符串

为了实现题目中要求,建立了几个自定义函数

计数函数  static void count(List l)

1 static void count(Listl){2 for(int i=0;i

其中List.add(String str)往list中添加str。List.get(int index)用于获得对象。

判断字符串是否是数字有这么几种方法:

1.使用Character.isDigit(char)判断

1 char num[] = str.toCharArray();//把字符串转换为字符数组

2 StringBuffer title = new StringBuffer();//使用StringBuffer类,把非数字放到title中

3 StringBuffer hire = new StringBuffer();//把数字放到hire中

4 for (int i = 0; i < num.length; i++) {5 //判断输入的数字是否为数字还是字符

6 if (Character.isDigit(num[i])) {把字符串转换为字符,再调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False7 hire.append(num[i]);//如果输入的是数字,把它赋给hire} else {title.append(num[i]);//如果输入的是字符,把它赋给title}}}

2.使用类型转换判断

1 try {String str="123abc";2 int num=Integer.valueOf(str);//把字符串强制转换为数字

3 return true;//如果是数字,返回True

4 } catch(Exception e) {5 return false;//如果抛出异常,返回False}

String str = "";boolean isNum = str.matches("[0-9]+");

//+表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7")

ps:这个方法只能用于判断是否是正整数

笔者程序里直接使用了第二种方法:

1 static booleanisnumber(String a){2 try{3 Integer.parseInt(a);//数字字符串转换int型数字 “123”->123

4 return true;5 } catch(Exception e) {6 return false;7 }8 }//判断是否为数字

Integer.parseInt(a)函数,如果a中含有非数字,就会抛出异常。return false。

排序函数是调用了collection下的一个sort自带函数【很好用!】

1 //Collections.sort排序

2 Collections.sort(number);3 Collections.sort(word);

这样的话,number和word直接变成了有序从小到大排列的list。

排序其实还有一种方法,是通过调用compare函数。

完整程序:

1 importjava.util.ArrayList;2 importjava.util.Collections;3 importjava.util.List;4 importjava.util.Scanner;5

6

7 public classclasstest {8

9

10 static List number=new ArrayList();11 static List word=new ArrayList();12

13

14 static void count(Listl){15 for(int i=0;i

23

24

25 static booleanisnumber(String a){26 try{27 Integer.parseInt(a);//数字字符串转换int型数字 “123”->123

28 return true;29 } catch(Exception e) {30 return false;31 }32 }//判断是否为数字

33

34

35

36 public static voidmain(String[] args) {37

38 System.out.println("please input the string");39 Scanner get=newScanner(System.in);40 String str=get.nextLine();41 System.out.println("string is "+str);//键盘获取字符串

42

43 java.util.List list=new ArrayList();//problem?

44

45 String[] text = str.split(" ");46 for(int i=0;i

49

50

51 classtest.count(list);52

53 //Collections.sort排序

54 Collections.sort(number);55 Collections.sort(word);56 System.out.println("number sort:"+number);57 System.out.println("word sort:"+word);58 }59

60 }

程序其实不难,但是由于自身对java的不熟悉,折腾了很久【差点砸电脑……】

程序运行结果:

104369f44f4a442e620ce0b952e0c209.png

好了……宝宝继续做下一道题……

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值