0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArrayListで独自に作成したクラスを扱うメモ

Last updated at Posted at 2020-05-07

#ArrayListで独自に作成したオブジェクトを扱う
はじめまして。はねうまです。
自作のクラスのリストを扱うときのメモを書いてみます。
例:自作クラス「Food」のリストを扱う
addメソッドの引数の中でFoodをインスタンス化し、リストに追加する。

List<Food> foodList = new ArrayList<>();
foodList.add(new Food("バナナ","果物")); 
foodList.add(new Food("キャベツ","野菜"));
foodList.add(new Food("カレー","料理"));

要素にアクセスするには・・・
(1)Foodクラスの変数を用意して、そこにリストからgetして代入する
(2)その変数を使って自作クラスの処理をする
(3)拡張for文の利用

Food fd = foodList.get(0);  // (1)
fd.printFood();  // (2)
fd.setFoodName("りんご");  // (2)

for(Food f : foodList) {  // (3)
    f.printFood();
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?