配列から重複要素を取り除いてソート
# 配列から重複要素を取り除いてソート sub getUniqueArray{ my @items = @_; my %seen; my @uniq; foreach my $item (@items){ push(@uniq, $item) unless $seen{$item}++; } return sort @uniq; }
自動販売機クラス
自動販売機のクラス。ある程度の機能を付け足しただけなので、ちょい汚い。
- Articleを配列化、add追加するようにする
- System.out.printlnもMainはもたないようにする
public class Article{ private String name; private int price; public Article(String name, int price){ this.name = name; this.price = price; } public String getName(){ return this.name; } public int getPrice(){ return this.price; } }
public class Automat{ private int sum = 0; private boolean isLamp = false; private Article article = new Article("珈琲",120); private int[] coins = {1, 10, 50, 100, 500}; private int[] change = {}; public void coinIn(int in){ this.sum += in; checkLamp(); } public int getSum(){ return this.sum; } public String pushButton(){ if(this.isLamp){ this.sum -= this.article.getPrice(); return this.article.getName(); } checkLamp(); return "足りません"; } public void checkLamp(){ if(this.sum >= this.article.getPrice()){ this.isLamp = true; } else { this.isLamp = false; } } public void receiveChange(){ System.out.println("ジャラジャラ〜"); for(int i = this.coins.length-1; this.sum>0;){ if(this.coins[i] > this.sum){ i--; } else { this.sum -= this.coins[i]; System.out.println(coins[i]); } } System.out.println("毎度"); } }
public class Main{ public static void main(String[] args){ //Coin[] coins = new Coin[9]; Automat automat = new Automat(); System.out.println(automat.getSum()); automat.coinIn(50); System.out.println(automat.pushButton()); System.out.println(automat.getSum()); automat.coinIn(100); System.out.println(automat.pushButton()); System.out.println(automat.getSum()); System.out.println(automat.receiveChange()); } }
2つの長方形の衝突判定
public class Rectangle{ private double x;//位置x private double y;//位置y private double w;//幅x private double h;//高さx public Rectangle(){ System.out.println("Rectangle create default"); this.x = 10; this.y = 10; this.w = 70; this.h = 80; } public Rectangle(double x, double y , double w , double h ){ System.out.println("Rectangle create custom"); this.x = x; this.y = y; this.w = w; this.h = h; } public String toString(){ return "X:"+this.x+ " Y:"+this.y + "W:"+this.w+ " H:"+this.h; } public double getX(){ return this.x; } public double getY(){ return this.y; } public double getW(){ return this.w; } public double getH(){ return this.h; } public void setX(double x){ this.x = x; } public void setY(double y){ this.y = y; } public void setW(double w){ this.w = w; } public void setH(double h){ this.h = h; } public Boolean isTouch(Rectangle r1, Rectangle r2){ if(((r1.x < r2.x + r2.w) && (r1.y < r2.y + r2.h)) && ((r2.x < r1.x + r1.w) && (r2.y < r1.y + r1.h)) ){ return true; } return false; } }
import java.io.*; public class Main{ public static void main(String args[]){ int n; Rectangle pnt1 = new Rectangle(); Rectangle pnt2 = new Rectangle(0, 0, 0, 0); System.out.println("pnt1 " + pnt1.toString()); System.out.println("pnt2 " + pnt2.toString()); System.out.println(pnt1.isTouch(pnt1, pnt2)); } }
テキストファイルの入出力
FileReaderTest.java
import java.io.*; class FileReaderTest { public static void main(String[] args) { try { FileReader in = new FileReader("file.txt"); int ch; while ((ch = in.read()) != -1) { System.out.print((char)ch); } in.close(); } catch (IOException e) { System.out.println(e); } } }
file.txt
abcdefghijklmn あいうえおかきくけこ
whileのループ内では読み込んだテキストをread()で1文字ごとに区分けして標準出力している。
ここで読み込まれたchはint型の文字コードのため、
文章として表示するには(char)でキャスト(型変換)する必要がある。
仮にキャストを忘れると、次のような出力になる。
(文字コードをかわりやすくするため、1文字ごとに空白をいれてあります)
System.out.print(ch + " ");
出力結果
97 98 99 100 101 102 103 104 105 106 107 108 109 110 13 10 12354 12356 12358 12360 12362 12363 12365 12367 12369 12371 13 10
Java5.0以降の配列
ArrayList
import java.util.*; class ListTest { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("AAA"); list.add("BBB"); list.add("CCC"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
上記のようなコードをコンパイラすると、
注:ListTest.java の操作は、未チェックまたは安全ではありません。 注:詳細については、-Xlint:unchecked オプションを指定して再コンパイルしてください。
のようなエラーが表示され、オプションをつけると詳細なエラー内容が表示される。
ListTest.java:6: 警告:[unchecked] raw 型 java.util.ArrayList のメンバとしての add(E) への無検査呼び出しです。 list.add("AAA");
これを解決するためには以下のようなコードで配列を宣言しなければならない。
import java.util.*; class ListTest { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("AAA"); list.add("BBB"); list.add("CCC"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
クラス型名の直後に<String>のように配列内部の要素の型を追記する必要がある。
HashMap
ハッシュについても同様で、<String,String>のようにキーと値の型をカンマ(,)区切りで指定する。
内部の値が指定した型と異なる場合は、キャスト(型変換)する必要がある。
import java.util.*; class ListTest { public static void main(String[] args) { HashMap<String,String> map = new HashMap<String,String>(); map.put("Name", "Tanaka"); map.put("Age", String.valueOf(26)); System.out.println("Name = " + map.get("Name")); System.out.println("Age = " + map.get("Age")); } }
配列の和集合と積集合
和集合
2つの配列、どちらかに入っている要素 (重複除く)
積集合
2つの配列、どちらにも存在する要素
my @a = (1,3,6,7,9,); my @b = (1,4,5,6,8,9,); my %union, %isect; foreach my $e (@a, @b){ $union{$e}++ && $isect{$e}++; } my @union = keys %union; my @isect = keys %isect; print "@union \n"; # 1 3 4 5 6 7 8 9 # (ただし、順番がバラバラの可能性がある) print "@isect \n"; # 1 6 9