题目:
统计一个字符串中数字和字符串的个数,并分别进行排列,要求
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<String> l = new ArrayList<String>();// 利用ArrayList类实例化List集合
但是!在笔者的eclipse中,如果是在main函数中申明的话,需要写全,不然会出现很美妙的红色波浪线【笔者在这里纠结了好久好久。。。。】
java.util.List<String> list=new ArrayList<String>();
但是在public class中就直接申明就好
1 2 static List<String> number=new ArrayList<String>();3 static List<String> word=new ArrayList<String>();4
这里是申明了两个string型的list,分别用来存放字符串中的数字和字符串
为了实现题目中要求,建立了几个自定义函数
计数函数 static void count(List<String> l)
1 static void count(List<String> l){2 for(int i=0;i<l.size();i++){3 if(isnumber(l.get(i))){4 number.add(l.get(i));5 }else word.add(l.get(i));6 }7 System.out.println("NUMBERCOUNT: "+number.size());8 System.out.println("WORDCOUNT: "+word.size());9 }//统计字符串和数字的个数
其中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;//如果是数字,返回True4 } catch (Exception e) {5 return false;//如果抛出异常,返回False}
3.使用正则表达式判断
String str = "";boolean isNum = str.matches("[0-9]+");
//+表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7")
ps:这个方法只能用于判断是否是正整数
笔者程序里直接使用了第二种方法:
1 static boolean isnumber(String a){2 try {3 Integer.parseInt(a);//数字字符串转换int型数字 “123”->1234 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 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.List; 4 import java.util.Scanner; 5 6 7 public class classtest { 8 9 10 static List<String> number=new ArrayList<String>();11 static List<String> word=new ArrayList<String>();12 13 14 static void count(List<String> l){15 for(int i=0;i<l.size();i++){16 if(isnumber(l.get(i))){17 number.add(l.get(i));18 }else word.add(l.get(i));19 }20 System.out.println("NUMBERCOUNT: "+number.size());21 System.out.println("WORDCOUNT: "+word.size());22 }//统计字符串和数字的个数23 24 25 static boolean isnumber(String a){26 try {27 Integer.parseInt(a);//数字字符串转换int型数字 “123”->12328 return true;29 } catch (Exception e) {30 return false;31 }32 }//判断是否为数字33 34 35 36 public static void main(String[] args) {37 38 System.out.println("please input the string");39 Scanner get=new Scanner(System.in);40 String str=get.nextLine();41 System.out.println("string is "+str);//键盘获取字符串42 43 java.util.List<String> list=new ArrayList<String>();//problem?44 45 String[] text = str.split(" ");46 for(int i=0;i<text.length;i++){47 list.add(text[i]);48 }//存入list49 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的不熟悉,折腾了很久【差点砸电脑……】
程序运行结果:
好了……宝宝继续做下一道题……
原标题:对字符串进行简单的字符数字统计 探索java中的List功能
关键词:JAVA