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

时间:2022-06-28 08:03:16

题目:

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

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中就直接申明就好

     static List<String> number=new ArrayList<String>();
static List<String> word=new ArrayList<String>();

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

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

计数函数  static void count(List<String> l)

     static void count(List<String> l){
for(int i=0;i<l.size();i++){
if(isnumber(l.get(i))){
number.add(l.get(i));
}else word.add(l.get(i));
}
System.out.println("NUMBERCOUNT: "+number.size());
System.out.println("WORDCOUNT: "+word.size());
}//统计字符串和数字的个数

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

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

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

 char num[] = str.toCharArray();//把字符串转换为字符数组
StringBuffer title = new StringBuffer();//使用StringBuffer类,把非数字放到title中
StringBuffer hire = new StringBuffer();//把数字放到hire中
for (int i = 0; i < num.length; i++) {
// 判断输入的数字是否为数字还是字符
if (Character.isDigit(num[i])) {把字符串转换为字符,再调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False
hire.append(num[i]);// 如果输入的是数字,把它赋给hire} else {title.append(num[i]);// 如果输入的是字符,把它赋给title}}}

2.使用类型转换判断

 try {String str="123abc";
int num=Integer.valueOf(str);//把字符串强制转换为数字
return true;//如果是数字,返回True
} catch (Exception e) {
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:这个方法只能用于判断是否是正整数

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

     static boolean isnumber(String a){
try {
Integer.parseInt(a);//数字字符串转换int型数字 “123”->123
return true;
} catch (Exception e) {
return false;
}
}//判断是否为数字

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

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

     //Collections.sort排序
Collections.sort(number);
Collections.sort(word);

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

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

完整程序:

 import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; public class classtest { static List<String> number=new ArrayList<String>();
static List<String> word=new ArrayList<String>(); static void count(List<String> l){
for(int i=0;i<l.size();i++){
if(isnumber(l.get(i))){
number.add(l.get(i));
}else word.add(l.get(i));
}
System.out.println("NUMBERCOUNT: "+number.size());
System.out.println("WORDCOUNT: "+word.size());
}//统计字符串和数字的个数 static boolean isnumber(String a){
try {
Integer.parseInt(a);//数字字符串转换int型数字 “123”->123
return true;
} catch (Exception e) {
return false;
}
}//判断是否为数字 public static void main(String[] args) { System.out.println("please input the string");
Scanner get=new Scanner(System.in);
String str=get.nextLine();
System.out.println("string is "+str);//键盘获取字符串 java.util.List<String> list=new ArrayList<String>();//problem? String[] text = str.split(" ");
for(int i=0;i<text.length;i++){
list.add(text[i]);
}//存入list classtest.count(list); //Collections.sort排序
Collections.sort(number);
Collections.sort(word);
System.out.println("number sort:"+number);
System.out.println("word sort:"+word);
} }

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

程序运行结果:

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

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