String
| length() | 長さを返す |
| indexOf(文字列) | 検索。見つかった位置(0~)を返す。存在しなければ-1。 |
| indexOf(文字列,開始位置) | 検索を開始位置から行う。 |
| lastIndexOf(文字列) | 後ろから検索。見つかった位置(0~)を返す。存在しなければ-1。 |
| substring(開始,終了) | 部分文字列を返す。開始位置から終了位置の1つ前まで。 |
| charAt(位置) | その位置の1文字を返す。 |
| contains(文字列) | 文字列を含んでいればtrueを返す。 |
| equals(文字列) | 同じならtrueを返す。 |
| replace(検索文字列,変更文字列) | 検索文字列を変更文字列に置換 |
| startWidth(文字列) | 文字列で始まるならtrueを返す。 |
| endsWidth(文字列) | 文字列で終わるならtrueを返す。 |
| split(区切り文字) | 区切り文字で文字列を分割し配列で返す |
Math
全てstaticメソッド。
| max(数1,数2) | 大きい方を返す |
| min(数1,数2) | 小さい方を返す |
| abs(数) | 絶対値を返す |
| ceil(数) | 切り上げ |
| floor(数) | 切り捨て |
| round(数) | 四捨五入 |
| random() | 0~1未満の乱数を返す。例:1~6の乱数 (int)(random()*6)+1 |
StringBuilder
文字列の連結
| append(文字列) | 文字列をつなげる |
| insert(位置,文字列) | 位置に文字列を挿入 |
| reverse() | 逆順にする |
Scanner
文字列入力の例
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
| nextInt() | 整数の入力 |
| nextLine() | 文字列の入力 |
File
ファイルの操作
File file = new File(ファイル名);
| length() | ファイルのサイズをlong型で返す |
| exists() | 存在するならtrue |
| createNewFile() | ファイル新規作成 |
| mkdir() | フォルダ新規作成 |
| delete() | ファイル削除 |
| renameTo() | ファイル名変更 |
| list() | ファイル一覧取得。Stringの配列を返す。 |
| listFiles() | ファイル一覧取得。Fileの配列を返す。 |
| getName() | ファイル名を返す。 |
| getPath() | フルパスを返す。 |
| isDirectory() | フォルダならtrueを返す。 |
| isFile() | ナイルならtrueを返す。 |
ファイルの読み書き
読み込み
try {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
} catch (IOException e) {
}
書き込み
try {
FileWriter fw = new FileWriter("test.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println("abc");
pw.close();
} catch (IOException e) {
}
List
追加・削除可能な配列
| ArrayList | 配列。要素の参照が速い。 |
| LinkedListList | リスト。挿入・削除が速い。 |
生成
List<型> list = new ArrayList<型>();
Stringの例:
List<String> list = new ArrayList<String>();
| add(要素) | 要素の追加 |
| get(番号) | その番号の要素を返す |
| set(番号,要素) | その番号を指定要素にする |
| size() | 要素数を返す |
| remove(番号) | その番号の要素を削除 |
| clear() | 全要素を削除 |
| isEmpty() | 要素が一つも無いならtrue |
| contains(要素) | その要素があるならtrue |
| indexOf(要素) | その要素があるなら番号を返す。無いなら-1。 |
Set
重複しない配列。順序は存在しない。
| HashSet | 順番不定。 |
| TreeSet | 昇順に値を取り出せる。 |
| LinkedHashSet | 挿入順に値を取り出せる。 |
生成
Set<型> list = new HashSet<型>();
Stringの例:
Set<String> list = new HashSet<String>();
| add(要素) | 要素の追加 |
| size() | 要素数を返す |
| remove(要素) | その要素を削除 |
| clear() | 全要素を削除 |
| isEmpty() | 要素が一つも無いならtrue |
| contains(要素) | その要素があるならtrue |
Map
キーと値のペアの配列
| HashMap | 順番不定。 |
| TreeMap | キーの昇順に値を取り出せる。 |
生成
Map<キーの型,値の型> list = new HashMap<キーの型,値の型>();
キーがString、値がIntegerの例:
Map<String,Integer> list = new HashMap<String,Integer>();
| put(キー,値) | 要素の追加・設定 |
| get(キー) | そのキーの要素を返す |
| size() | 要素数を返す |
| remove(キー) | そのキーの要素を削除 |
| clear() | 全要素を削除 |
| isEmpty() | 要素が一つも無いならtrue |
| containsKey(キー) | そのキーがあるならtrue |
| containsValue(キー) | その値があるならtrue |
| keySet() | キーのSetを取得。 |
| values() | 値のコレクションを取得。 |
Collections
// 要素を全て追加
Collections.addAll(list,5,8,9,3,7);
// リストを逆順に
Collections.reverse(list);
// リストの最大値・最小値
int n = Collections.max(list);
int n = Collections.min(list);
// リストを整列
Collections.sort(配列);
// リストをシャッフル
Collections.shuffle(list);
Arrays
// 配列をリストにして返す
List list = Arrays.asList(配列);
// 配列の整列
Arrays.sort(配列);