最終更新 2024.04.01
public class Hello{
public static void main(String[] args){
System.out.println("Hello");
System.out.print("World" + "\n");
}
}
import java.io.*;
BufferedRead reader = new BufferedReader(new InputStreamReader(System.in),1)
try{
String line = reader.readreadLine();
} catch ( IOException e){
System.out.println(e);
} catch ( NumberFormatException e){
System.out.println("Input is no correct");
}
int i = Integer.parseInt(String line);
int d = Double.parseDouble(String line);
String line = String.valueOf(i);
String line = String.valueOf(d);
int len;
char cha;
String str,str2,str3,str4;
str = "yabu";
cha = str.charAt(3); 3 文字目の文字
len = str.length(); 長さ
str2 = str.toLowerCase(); 小文字に変換
str3 = str.substring(3); 4 文字目以降の部分文字列
str4 = str.substring(1,3); 2 文字目から 3 文字目まで
toString() 文字列自体を返す
trim() 両端からスペースを取り除いた文字列
replace("a","b") 置換
行分解
String str, str2[];
str = "123\n456\n\n789\n";
str2 = str.split("\n");
for(i = 0; i < str2.length; i++){
textview.append(str2[i] + "\n");
}
結果
str2[0] = "123";
str2[1] = "456";
str2[2] = "";
str2[3] = "789";
行末の "\n" はあってもなくても結果は同じ。
\n\n の部分は "" があると見なされる。
break の後にラベル名を書く
public static int halve(int n) public : 他のクラスから参照可能 static : クラスメソッド or インスタンスメソッド int : 帰り値の型 何も返さないときは void
1 次元配列
int[] a;
a = new int[3];
配列のサイズ : a.length
配列の初期化 int[] a = { 1 , 2 , 3 };
あるいは a = new int[]{ 1, 2 , 3 };
2 次元配列
int[][] a;
a = new int[2][3];
int[][] a = {
{ 1 , 2 , 3 },
{ 4 , 5 , 6 },
};
a.length の値は 2
a[0].length の値は 3
2 次元配列の場合、各要素の配列の長さは一定でなくてもよい
C の場合と同じ
html 側で <applet code="Hello.class" width=400 height=200> </applet>
Java 側では
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet {
グローバル変数の宣言
public void init(){
}
public void start(){
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
描画処理
}
}
スーパークラスのコンストラクタは継承されない。 サブクラスのコンストラクタの最初で、スーパークラスの 引数なしのコンストラクタが呼び出される。 明示的に引数付きのコンストラクタを呼び出したいときは、 サブクラスのコンストラクタで super(30,20); などと記述する super() が最初に来ない場合は、super() が最初に自動的に 挿入される。 this(20); 自分の引数付きコンストラクタの呼び出し
1. Thread クラスを extends して新しいクラスを作る
2. そのインスタンスを作成する
3. start() メソッドで分岐すし、run() メソッドが実行される
スレッドの定義
public class YThread extends Thread {
public int switch; // メインからの命令を受け取る変数
YTread(){
}
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("InterruptedException", e.getMessage());
}
}
public void re_init(){ // 何らかの設定など
}
}
メイン側
YThread th = new YTread();
th.start(); // run が実行される
th.switch = 1; // 何らかの設定
th.re_init(); // 何らかの設定
package yabu; とすると、パッケージ yabu に属するクラスとなる。 そのパッケージを使うには、yabu というディレクトリを 作り、そのディレクトリの中に .class ファイルを配置する。 setenv CLASSPATH /home/yabu/java:デフォルトのパス などと打つと、パッケージを探す基点となるディレクトリを 指定することができる。カレントの下にあるときは、CLASSPATH は なくても良い。
PrintWriter クラスを使うと printf が使え、フォーマット付きの
出力ができる。
Java
import java.io.*;
// FileOutputStream --- OutputStreamWriter --- PrintWriter
String fname = "a.txt";
try {
PrintWriter w = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(fname),"Shift_JIS"));
w.printf("%02d:%02d\n",1,-2);
w.println("yabu");
w.print("tetsuro\n");
w.flush();
w.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e){
System.out.println(e);
}
Android #1
FileOutputStream のオブジェクトの作成法は 2 つある
FileOutputStream outs = new FileOutputStream(fname); // 通常
FileOutputStream outs = openFileOutput(fname, MODE_PRIVATE); // モード指定
// FileOutputStream -- OutputStreamWriter --- PrintWriter
try {
FileOutputStream outs = openFileOutput(fname, MODE_PRIVATE);
PrintWriter w = new PrintWriter(new OutputStreamWriter(outs,"UTF-8"));
w.write(contents.toString());
w.close();
outs.close();
} catch (FileNotFoundException ex) {
Log.e("FileNotFoundException", ex.getMessage());
} catch (IOException ex){
Log.e("IOException", ex.getMessage());
}
Android #2
// FileOutputStream --- OutputStreamWriter -- BufferedWriter
try {
FileOutputStream outs = new FileOutputStream(fname);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
w.write(str.toString());
w.flush();
w.close();
outs.close();
} catch (FileNotFoundException ex) {
Log.e("FileNotFoundException", ex.getMessage());
} catch (IOException ex){
Log.e("IOException", ex.getMessage());
}
String fname = "in.txt";
try {
BufferedReader r = new BufferedReader(new InputStreamReader(
new FileInputStream(fname),"UTF-8"));
String line;
while(( line = r.readLine()) != null ){
System.out.println(line);
}
r.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e){
System.out.println(e);
}
ファイルの終端まで来たときには、null を返す。
class Myclass {
int value;
static int common_value;
Myclass(int val){
value = val;
common_value = val * 2;
}
public int get_value() {
return ( this.value );
}
public static int get_value_static(Myclass obj){
return ( obj.value );
}
public int get_common_value() {
return( common_value );
}
public static int get_common_value_static() {
return( common_value );
}
}
static が付いたフィールドは 1 つだけ生成される。
static が付かないフィールドはインスタンス毎に生成される。
static が付かないメソッドは、そのインスタンスへのポインタが
暗黙の了解として渡される。ゆえに、単にフィールド名を指定
すると、this が付いているとみなされる。
static が付いたメソッドは暗黙のポインタが渡されないので、
当然 static でないフィールドの値にはアクセスできない。
上の例のように明示的にインスタンスを引数として取る必要が
ある。
static が付いたメソッドは、インスタンスを未作成の状態で、
クラス名.メソッド名
という呼び方が可能。ただし通常の
インスタンス名.メソッド名
も可能。
MyClass myclass; myclass = new Myclass(引数); 宣言して new していない状態では myclass == null
C の関数に相当するものを作るにはどうすればよいのだろう?
関数だけ集めたクラスを作り、
Class lib {
static public void func1(a, b){
}
}
のように、全て static にすれば、
lib.func1(a, b);
のようにどこからでも呼べるが・・・
import java.lang.Math; <--- android studio では不要 double Math.floor(x) 切り捨て double Math.ceil(x) 切り上げ int Math.round(x) 四捨五入 floor -1.0 -- -0.001 --> -1.0 0.0 -- 0.999 --> 0.0 1.0 -- 1.999 --> 1.0 ceil -1.999 -- -1.000 --> -1.0 -0.999 -- 0.000 --> -0.0 0.001 -- 1.000 --> 1.0 round -1.499 -- -0.500 --> -1.0 -0.499 -- 0.499 --> 0.0 0.500 -- 1.499 --> 1.0
sprintf 相当
String str;
str = String.format("%02d:%02d:%02d",hour,minute,second);
printf 相当
System.out.printf("%02d:%02d:%02d\n",hour,minute,second);
fprintf 相当
PinterWriter w = new PrinterWriter(new OutputStreamWriter(outs, "Shift_JIS"));
w.printf(
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int ms = calendar.get(Calendar.MILLISECOND);
String line = String.format("time : %04d/%02d/%02d %02d:%02d:%02d\n",year,month+1,day,hour,minute,second);