久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java:如何獲取迭代器<字符>從字符串

Java: how to get Iteratorlt;Charactergt; from String(Java:如何獲取迭代器lt;字符gt;從字符串)
本文介紹了Java:如何獲取迭代器<字符>從字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要一個來自 String 對象的 Iterator<Character>.Java 中是否有任何可用函數可以為我提供此功能,還是我必須自己編寫代碼?

I need a Iterator<Character> from a String object. Is there any available function in Java that provides me this or do I have to code my own?

推薦答案

一種選擇是使用 番石榴:

ImmutableList<Character> chars = Lists.charactersOf(someString);
UnmodifiableListIterator<Character> iter = chars.listIterator();

這會生成一個由給定字符串支持的不可變字符列表(不涉及復制).

This produces an immutable list of characters that is backed by the given string (no copying involved).

如果您最終自己這樣做,我建議不要像許多其他示例那樣公開 Iterator 的實現類.我建議改為創建自己的實用程序類并公開靜態工廠方法:

If you end up doing this yourself, though, I would recommend not exposing the implementation class for the Iterator as a number of other examples do. I'd recommend instead making your own utility class and exposing a static factory method:

public static Iterator<Character> stringIterator(final String string) {
  // Ensure the error is found as soon as possible.
  if (string == null)
    throw new NullPointerException();

  return new Iterator<Character>() {
    private int index = 0;

    public boolean hasNext() {
      return index < string.length();
    }

    public Character next() {
      /*
       * Throw NoSuchElementException as defined by the Iterator contract,
       * not IndexOutOfBoundsException.
       */
      if (!hasNext())
        throw new NoSuchElementException();
      return string.charAt(index++);
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}

這篇關于Java:如何獲取迭代器&lt;字符&gt;從字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 精品国产久 | 欧美精品第一区 | 欧美一区二区三区电影 | 超碰3 | 精品国产不卡一区二区三区 | 久久成人一区 | 日本中文字幕在线观看 | 久久久久久久一区 | 欧美 日韩 综合 | 日韩在线视频一区 | 中国黄色在线视频 | 看av片网站 | 成年人的视频免费观看 | 亚洲一区二区精品 | 国产日韩欧美电影 | 一区二区三区精品视频 | 欧美在线一区二区三区 | 欧美11一13sex性hd | 一级毛片免费完整视频 | 欧美aaa一级片 | 久久久99精品免费观看 | 国内精品视频在线 | 日本三级线观看 视频 | 国产美女一区二区 | 国产精品亚洲综合 | 亚洲www啪成人一区二区麻豆 | 999国产精品视频 | 高清黄色毛片 | 免费国产视频在线观看 | 亚洲国产精品久久久久秋霞不卡 | 久久久久香蕉视频 | 翔田千里一区二区 | 日韩欧美在线观看视频网站 | 国产一区高清 | www.国产一区| 国内精品视频在线 | tube国产 | 亚洲性视频 | 久久久久久久久久久久一区二区 | 日韩欧美手机在线 | 天天艹天天干天天 |