如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
int engishCount; int chineseCount; int digitCount; for(int
i=0;i<str.length;i++) { char ch = str.charAt(i); if(ch>=’0’ && ch<=’9’) {
digitCount++ } else if((ch>=’a’ && ch<=’z’) || (ch>=’A’ && ch<=’Z’)) {
engishCount++; } else { chineseCount++; } } System.out.println(……………);
怎么检查一个字符串只包含数字?

 

一、用JAVA自带的函数

二、用正则表达式

三、用ascii码判断

 

Java 中如何利用泛型写一个 LRU 缓存?

 

这是一种混合的数据结构,我们需要在哈希表的基础上建立一个链表。但是Java已经为我们提供了这种形式的数据结构-LinkedHashMap!它甚至提供可覆盖回收策略的方法。唯一需要我们注意的事情是,改链表的顺序是插入的顺序,而不是访问的顺序。但是,有一个构造函数提供了一个选项,可以使用访问的顺序。
import java.util.LinkedHashMap; import java.util.Map; public LRUCache<K, V>
extends LinkedHashMap<K, V> {   private int cacheSize;   public LRUCache(int
cacheSize) {     super(16, 0.75, true);     this.cacheSize = cacheSize;   }
  protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {     return
size() >= cacheSize;   } }
写一段 Java 程序将 byte 转换为 long?
/**      * 利用 {@link java.nio.ByteBuffer}实现byte[]转long      * @param input
     * @param offset      * @param littleEndian 输入数组是否小端模式      * @return
     */     public static long bytesToLong(byte[] input, int offset, boolean
littleEndian) {         // 将byte[] 封装为 ByteBuffer         ByteBuffer buffer =
ByteBuffer.wrap(input,offset,8);         if(littleEndian){             //
ByteBuffer.order(ByteOrder) 方法指定字节序,即大小端模式(BIG_ENDIAN/LITTLE_ENDIAN)
            // ByteBuffer 默认为大端(BIG_ENDIAN)模式
            buffer.order(ByteOrder.LITTLE_ENDIAN);         }         return
buffer.getLong();       }
在不使用 StringBuffer 的前提下,怎么反转一个字符串?
String str=in.nextLine(); char str1[]; char[] chrCharArray;
//创建一个字符数组chrCharArray chrCharArray = str.toCharArray();//将字符串变量转换为字符数组 int
len=str.length(); str1=new char[len]; for (int i =0,j=len-1; i < len&&j>=0;j--,
i++) {     str1[i]=chrCharArray[j]; } System.out.println(str1); str=
String.valueOf(str1 ); //将字符数组转换为字符串 System.out.println(str);
Java 中,怎么获取一个文件中单词出现的最高频率?

 
public static void main(String[] args) {         long start =
System.currentTimeMillis();         String str = "Look to the skies above
London and you'll see the usual suspects rainclouds, plane and pigeons. But by
the end of the year, you might just see something else.";         str =
str.replace('\'', ' ');//将'号用空格替换         str = str.replace(',', '
');//将逗号用空格替换         str = str.replace('.', ' ');//将句号用空格替换         String[]
strings = str.split("\\s+");   // “\\s+”代表一个或多个空格,是正则表达式 //      String[]
strings = str.split(" +"); // “ +”在我的机器上也能代表一个或多个空格         Map<String,
Integer> map = new HashMap<String, Integer>();         List<String> list = new
ArrayList<String>();//存储每个不重复的单词         for(String s : strings){
            if(map.containsKey(s)){//如果map中已经包含该单词,则将其个数+1                 int
x = map.get(s);                 x++;                 map.put(s, x);
            }else{  //如果map中没用包含该单词,代表该单词第一次出现,则将其放入map并将个数设置为1
                map.put(s, 1);
                list.add(s);//将其添加到list中,代表它是一个新出现的单词             }         }
        int max=0;//记录出现次数最多的那个单词的出现次数         String maxString =
null;//记录出现次数最多的那个单词的值         /*          * 从list中取出每个单词,在map中查找其出现次数
         * 并没有真正排序,而只是记录下出现次数最多的那个单词          */         for(String s : list){
            int x = map.get(s);             if(x>max){
                maxString = s;                 max = x;             }         }
        System.out.println(maxString);         long end =
System.currentTimeMillis();         System.out.println("共耗时:" + (end - start) +
"毫秒");     }
 

如何检查出两个给定的字符串是反序的?

Java 中,怎么打印出一个字符串的所有排列?

Java 中,怎样才能打印出数组中的重复元素?

Java 中如何将字符串转换为整数?

在没有使用临时变量的情况如何交换两个整数变量的值?

技术
今日推荐
PPT
阅读数 99
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信