0
1

Java

Last updated at Posted at 2024-01-21

#クラス
public
どのクラスからもアクセスできるということ

static
クラス特有の値やメソッドということ

void
戻り値なし

public static int aaa(int a, int b){
  # int ==> 戻り値型
 # aaa ==> 関数名
 # () ==> 引数
}

#読み込み
##Scanner

import java.util.Scanner;

Scanner sc = new Scanner(System.in);

##next

# 標準入力 dog cat bird

A = sc.nextLine();
System.out.println(A);  # ==> dog cat bird
# 改行まで読み込む(String型)

B1 = sc.next();
B2 = sc.next();
System.out.println(B1);  # ==> dog
System.out.println(B2);  # ==> cat
# 空白まで読み込む(String型)

# 標準入力  10 200

C1 = sc.nextInt();
System.out.println(C1);  # ==> 10
# 空白まで読み込む(int型)

C2 = sc.nextDouble();
System.out.println(C2);  # ==> 200.0
# 空白まで読み込む(double型)

#型変換
##数値型→String型

# int ==> String
String a = String.valueOf(127);

# double ==> String
String a = String.valueOf(123.45);

# long ==> String
String a = String.valueOf(127l);

# boolean ==> String
String a = String.valueOf(false);

##String型→数値型

# String ==> int
int a = Integer.parseInt(123);

# String ==> double
double a = Double.parseDouble(123);

# String ==> long
long a = Long.parseLong(123);

# String ==> boolean
boolean a = Boolean.parseBoolean(True);
# "true""TRUE""True"など大文字小文字を区別しない"true"のみtrueとなる
# それ以外の値の場合は全てfalseとなる

数値型→数値型

int x = 3
double a = (double)x  # ==> a = 3.0
# 変換したい数値の前に(型名)

#文字列
##StringBuffer
StringBufferクラスは、Stringクラス同様に宣言した変数に文字列を格納する。
Stringクラスとの違いとして、変数に文字列を格納したあとでも「値を追加」「挿入」「変更」などの、文字列操作が可能である。
StringBufferクラスは、文字列の値が「不変である」と分かっているときに、使用するケースが多い。

StringBuffer sb = new StringBuffer("dog");
# sb ==> 変数名
# () ==> 文字列

# 引数の文字列表現をシーケンスに追加
sb.append(abc);  # ==> dogabc
# ()内の型は何でも良い

# シーケンスの部分文字列内の文字を削除
sb.delete(1, 4);  # ==> dbc

# シーケンス内の指定された位置にあるcharを削除
sb.deleteCharAt(0);  # ==> bc

# 文字列表現をシーケンス内の指定された位置に挿入
sb.insert(1, "yui");  # ==> byuic
# 挿入する文字列表現の型は何でも良い

# 文字シーケンスをシーケンスの順序を逆にしたもので置き換える
sb.reverse();  # ==> ciuyb

# シーケンスの指定された部分文字列内の文字を指定されたString内の文字で置き換える
sb.replace(1, 3, "o46");  # ==> co46yb

##比較

String a = dog;
String b = cat;

if(a.equals(b)){
  System.out.println("Yes");
}
else{
  System.out.println("No");
}
import java.util.Objects;

String a = dog;
String b = cat;

if(Objects.equals(a, b)){
  System.out.println("Yes");
}
else{
  System.out.println("No");
}

#演算
##割り算

Math.ceil(10.4)  # ==> 11.0
# 切り上げ
# 戻り値は double 

Math.floor(11.6)  # ==> 11.0
# 切り捨て
# 戻り値は double 

Math.round(12.5)  # ==> 13.0
# 四捨五入
# 戻り値は double 

##累乗

Math.pow(a, b)  # ==> a^b
# a, b  double 型のみ

#配列
##ソート

import java.util.Arrays;
import java.util.Collections;

int[] A = {1, 3, 4, 2, 5};

Arrays.sort(A);  # ==> {1, 2, 3, 4, 5}
# 昇順

Arrays.sort(A, Collections.reverseOrder());
# ==> {5, 4, 3, 2, 1}
# 降順
0
1
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
1