コレクション一覧 [/2010-02-13] リスト [/2023-09-24] マップ [/2023-09-24] セット [/2023-09-24] 総称型の使用 [/2007-06-11] 特殊なコレクション 空のコレクション [/2017-09-24] 1つだけ値を持つ不変コレクション [/2017-09-24] 変更不可能なコレクション [/2017-09-24] 動的型保証を行うコレクション [2008-12-08] 順序付きコレクション [2023-09-24]
Javaの標準API(java.*, javax.*)に含まれるメソッド名を分析して、よく使われている単語や接頭辞を抜き出してみました。 これで、もうメソッド名を決めるのに迷わない!はず…。 接頭辞 順位 単語 意味 代表例 出現回数 1 get 取得する List#get() 21198 2 set 設定する List#set() 8197 3 is 〜かどうか List#isEmpty() 4373 4 remove 取り除く List#remove() 2403 5 add 追加する List#add() 2213 6 create 作成する URI#create() 853 7 paint 描画する Component#paint() 731 8 update 更新する Component#update() 573 9 contains 含んでいるか List#contains()
久々にJavaを使うと忘れてたりするのでメモっとく。 対象のJavaはJava6です。 Javaはバージョンによってコーティングの常識が違うので注意が必要、 特にfor文は色々な書き方が出来るので非常に紛らわしい。 Listのfor文 // 例:ArrayList List<SomeClass> list = new ArrayList<SomeClass>(); // 拡張for文:推奨 for (SomeClass item : list) { // itemを処理 } // イテレータ:拡張for文が使えない場合に使用(要素の追加や削除が必要な場合など) for (Iterator<SomeClass> it = list.iterator(); it.hasNext(); ) { SomeClass item = it.next(); // itemを処理 } // インデックス:
コレクションを利用するTips イテレーション Iteratorを使う コレクションの中に格納された要素にアクセスする時は、Iteratorを使用することが通例です。その時のコーディングパターンで最も多い書き方は次のようにwhile文を使用するものです。 List persons; : Iterator it = persons.iterator(); while (it.hasNext()) { Person person = (Person)it.next(); // do something for person. } しかし、while文を使うとイテレータを示すローカル変数itの有効範囲がメソッド全体に渡ってしまいます。本来は、while文の内側に閉じて欲しいところです。また、複数のコレクションを操作する時に、ローカル変数名itを再度使いたいのですが、2度目は変数を宣言しないように
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く