43
56

More than 5 years have passed since last update.

Java チートシート

Last updated at Posted at 2018-05-22

目的

人は忘れるもの。
いろんな言語で書いていればなおさら。
書く前に目を通せばいろいろ捗るかも。

基本データ型 Data Type

説明 範囲
byte 整数8ビット -128~127
short 整数16ビット -32,768~32,767
int 整数32ビット -2,147,483,648~2,147,483,647
long 整数64ビット -9,223,372,036,854,775,808~9,223,372,036,854,775,807
char Unicode文字16ビット \u0000~\uFFFF
float 浮動小数点数32ビット (省略)
double 浮動小数点数64ビット (省略)
boolean - true, false

演算子 Operators

算術演算子 Arithmetic Operators

5 + 2;      // 7 加算
5 - 2;      // 3 減算
5 * 2;      // 10 乗算
5 / 2;      // 2 除算
5 % 2;      // 1 剰余

y = ++x;    // 前置 y = (x += 1);
y = x++;    // 後置 y = x; x = x + 1;

比較演算子 Comparison Operators

x < y;      // 未満
x <= y;     // 以下
x >= y;     // 以上
x > y;      // 超過
x == y;     // 同値
x != y;     // 同値ではない

論理演算子 Boolean Operators

x && y;     // AND
x || y;     // OR
!x;         // NOT

文字列 String

String str = "文字列";           // 初期化

str.length();                   // 長さ
str.subString(start, end);      // startからend-1の切り取り
str1.eq­ual­s(str2);              // 同値判定
str1.co­mpa­reT­o(str2);           // 比較
// str1 < str2 -> -1
// str1 == str2 -> 0
// str1 > str2 -> 1

char c = str.charAt(i);             // String -> char i番目の文字
char[] cs = str.toCharArray();      // String -> char 全体

型変換 Data Conversion

int i = Intege­r.p­ars­eIn­t(­str);        // int <- String
int i = (int) d;                      // int <- double

double d = Double.pa­rse­Dou­ble­(s­tr);   // double <- String
double d = (double) i;                // double <- int

String s = String.va­lue­Of(­i);         // String <- int
String s = String.va­lue­Of(­d);         // String <- double

数値処理 Math

Math.max(2, 5);     // 5 大きい方
Math.min(2, 5);     // 2 小さい方
Math.abs(-3);       // 3 絶対値

Math.ceil(1.5);     // 2.0 切り上げ
Math.floor(1.5);    // 1.0 切り捨て
Math.round(1.5);    // 2.0 四捨五入 

Math.pow(2, 10);    // 1024 累乗
Math.sqrt(2);       // 1.414… 平方根
Math.log10(1000);   // 3 常用対数

Math.PI             // π 円周率
Math.E              // e 自然対数の底

文字列整形 Formatter

そのうち書くかも。

制御構文 Statements

if

if ( i > 0 ) {
 ­ ;
} else if ( i < 0 ) {
 ­ ­;
} else {
 ­ ­;
}

for / for-each

for ( int i = 0; i < max; i++ ) {
 ­ ­;
}

for ( var : colle­ction ) {
 ­ ­;
}

while / do-while

while ( i > 0 ) {
 ­ ­;
}

do {
 ­ ­;
} while ( i > 0 );

switch

switch ( i ) {
 ­ case 0:
 ­ ­ ­ ­;
 ­ ­ ­ ­break;
 ­ case 1:
 ­ ­ ­ ­;
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ ­;
}

配列 Array

1次元配列

// 初期化
int[] num = new int[10];
Arrays.fill(num, 0);

// ディープコピー deep copy
String[] copySrc = new String[10];  // コピー元
String[] copyDst = new String[10];  // コピー先
copyDst = Arrays.copyOf(copySrc, copySrc.length);

多次元配列

// 初期化
int[][] num = new int[10][10];
for (int i = 0; i < num.length; i++) {
    Arrays.fill(num[i], 0);
}

// ディープコピー
String[][] copySrc = new String[10][10];    // コピー元
String[][] copyDst = new String[10][10];    // コピー先
for (int i = 0; i < copySrc.length; i++) {
    copyDst[i] = Arrays.copyOf(copySrc[i], copySrc[i].length);
}

リスト ArrayList

// 初期化
List<String> list = new ArrayList<String>();

// オブジェクトによる操作
list.add(str);      // 追加
list.remove(str);   // 削除
list.indexOf(str);  // インデックス取得
list.contains(str); // 存在チェック

// インデックスによる操作
list.get(i);        // i番目を取得
list.set(i, str);   // i番目を置換

ハッシュマップ HashMap

// 初期化
// key -> int
// value -> String
HashMa­p<Integer, St­rin­g> hMap =­ new HashMa­p<Integer, String­>();

hMap.pu­t(­i­, ­str);       // 追加
hMap.ge­t(­i);            // 取得
hMap.co­nta­ins­Key­(i);    // 存在チェック

優先度キュー PriorityQueue

PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
pQueue.offer(i);    // 追加
pQueue.peek(i);     // 取得(キューから削除しない)
pQueue.poll(i);     // 取得(キューから削除する)
43
56
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
43
56