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

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

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

問(wèn)題描述

我需要一個(gè)來(lái)自 String 對(duì)象的 Iterator<Character>.Java 中是否有任何可用函數(shù)可以為我提供此功能,還是我必須自己編寫(xiě)代碼?

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();

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

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

如果您最終自己這樣做,我建議不要像許多其他示例那樣公開(kāi) Iterator 的實(shí)現(xiàn)類(lèi).我建議改為創(chuàng)建自己的實(shí)用程序類(lèi)并公開(kāi)靜態(tài)工廠方法:

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();
    }
  };
}

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

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來(lái)自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 久久久精品一区二区三区四季av | 成人国产精品久久久 | h片在线看| 麻豆久久久久 | 亚洲综合色视频在线观看 | 亚洲a在线观看 | 中文字幕日本一区二区 | 成人不卡| 亚洲精品无人区 | 日日摸天天添天天添破 | 在线观看免费av网站 | 亚洲精品一区二区三区四区高清 | 九色综合网 | 中文字幕第5页 | 久久久久国产精品一区二区 | 国产成人免费 | 精品久久久久久久久久久久 | 日韩久久精品 | 狠狠色综合久久婷婷 | 亚洲成人中文字幕 | 亚av在线 | 日日综合 | 欧美一级欧美三级在线观看 | 久久久激情视频 | 精品96久久久久久中文字幕无 | 一区二区在线免费观看 | 久久国产成人 | 天堂一区二区三区四区 | 久久亚洲国产 | 日韩毛片免费视频 | 久久91av| 日本特黄a级高清免费大片 成年人黄色小视频 | 久久综合一区 | 久久乐国产精品 | 国产一区二区激情视频 | 五月婷婷婷 | 在线国产一区 | 国产精品黄 | 91精品国产91久久久久久不卞 | 日韩电影一区 | 亚洲视频自拍 |