Java

文字列から改行を削除する&設定した文字数だけ出力

配列の長さと、文字列の長さ、StringBufferの使い方 public static String deleteChangeLineAnd15CharDisplayed (String memo) { String s = memo; StringBuffer sb = new StringBuffer(); char[] c = s.toCharArray(); // 空の場合は空の文字列を返す if (s…

コマンドプロンプトのdir -sで取得したデータを整形したい

dir -sで取得したデータを C:\movie\adult\oppai とかにしてディレクトリ別に何処に何が入っているのか一目瞭然の形に整形するにはどうしたらいいのだろう。がんばる

テキストデータを読み込み、重複部分(重複行)以外を出力する。

概要 読み込んだテキストデータ(テキストファイル)を、重複部分(重複行)を削-除して出力する。 出力する際に、grepキーワードを含む行だけを抽出して出力する。 grepするキーワード数は2つ ロジック readyメソッド 入力するファイルのパスを指定 出力するフ…

チェーニング

import java.io.*; public class ReadAFile { public static void main(String[] args) { try { File myFile = new File("SampleText.txt"); // FileReaderはテキストファイルへの接続を行う接続ストリーム FileReader fileReader = new FileReader(myFile);…

シリアライゼーションとファイルのI/O

コード import java.io.*; public class WeightAndHeight implements Serializable{ // この変数の値は保存される private int weight; private int height; public void setWeight(int w) { weight = w; } public void setHeight(int h) { height = h; } pu…

isEmpty()けっこう便利かも

public class StringTest { public static void main(String[] args) { String body = ""; if (testStr.isEmpty()) { System.out.println("bodyがガラ空きだぜ!"); } } } これってけっこう便利だね。ファイルコピーするメソッドも作ってくれたらいいのに。I…

クラス Calendar の使い方

抽象クラスだってことを意識する import java.util.Calendar;public class CalendarTest { public static void main(String[] args) { // Calendar calendar = new Calendar(); // ↑のコードはコンパイルエラーになる // ⇒カレンダークラスは抽象クラスなの…

ラッパのユーティリティメソッド

public class StringToP { public static void main(String[] args) { String s = "2"; int x = Integer.parseInt(s); String sb = "true"; boolean b = new Boolean(sb).booleanValue(); if (b) { System.out.println(x); } } } 実行結果 2 Booleanクラスに…

ラッピング

public class Test { Integer i; int j; public static void main(String[] args) { new Test().go() ; } public void go () { j = i; System.out.println(j); System.out.println(i); } } 実行結果 1 1 ( ・ิω・ิ)ラッピングしとる!!

プリミティブ型とArrayList

メイン public class PrimitiveTestDrive { public static void main(String[] args) { new PrimitiveAndObjectTest(); } } import java.util.ArrayList;public class PrimitiveAndObjectTest { public PrimitiveAndObjectTest () { ArrayList listOfNumbers…

-スタティクイニシャライザを使ったstatic final変数の初期化の方法

public class RandomD { public static final double RANDOM_SIGN; // スタティクイニシャライザを使用して値を代入 static { RANDOM_SIGN = (double)Math.random(); System.out.println(RANDOM_SIGN); } } public class RandomDTestDrive { public static v…

staticメソッドからはstaticでない変数(インスタンス変数)は使えない

public class Number { // private int num; この書き方はエラー! private static int num; public static void main(String[] args) { System.out.println("Number is = " + num); } public void setSize (int n) { num = n; } public int getSize() { ret…

スーパークラスのコンストラクタに引数がある場合

スーパークラス public abstract class Animal { private String name; public String getName () { return name; } public Animal (String theName) { name = theName; } } サブクラス public class Monkey extends Animal{ public Monkey (String name) { …

サブクラスのコンストラクタを呼出す

Aの手 public class HandOfA { public HandOfA () { System.out.println("A「グー!!"); } } Bの手 public class HandOfB extends HandOfA{ public HandOfB () { System.out.print("B「パー!」"); } } main public class HandsTester { public static void…

同一クラスのオブジェクトを複数生成してハッシュ値(ハッシュコード)を調べる

※*1字下げが反映されてないorz 今度暇なときにでも直す 化け物アヒルと子アヒルクラス public class Duck { int size; // コンストラクタ public Duck (int ducksize) { size = ducksize; if (ducksize System.out.println("ピヨピヨ"); else System.out.pri…