Stringクラスと集合クラスノート

4954 ワード

一:StringクラスとStringBufferクラス
   1.Javaにあります.langパッケージにあります.   2.Stringクラスオブジェクトの内容は、一度初期化されると変更できません.   3.StringBufferクラスは、コンテンツが変更可能な文字列をカプセル化するために使用されます.   4.文字列定数(helloなど)は、実際には特殊な匿名Stringオブジェクトです.String s 1="hello";     String s2 = "hello";     String s1 = new String("hello");     String s2 = new String("hello");
s 1とs 2の2つの指向は同じオブジェクトではないので、出力された結果はfalseである.
   5.Stringクラスの共通メンバーメソッド(1).構造方法:String(byte[]bytes,int offset,int length)(2).equalslgnoreCaseメソッド:大文字と小文字を無視します.     (3). indexOf(int ch)方法(4).substring(int beginIndex)メソッド;           substring(int beginIndex,int endIndex);
例1:キーボード入力を1行ずつ読み込み、入力内容が「stop」になるまでプログラムを終了する.
public class ReadLine {
	public static void main(String[] args){
		byte[] buf = new byte[1024];
		String str = null;
		int pos =0;
		int ch = 0;
		System.out.println(" :");
		while(true){
			try{
				ch = System.in.read();
			}catch(Exception e){
				e.printStackTrace();
			}			
			switch(ch){
				case '\r':
					break;
				case '
': str = new String(buf,0,pos); if(str.equalsIgnoreCase("stop")){ return; }else{ System.out.println(str); pos=0; break; } default: buf[pos++] = (byte)ch; } } } }

例2:
public class Demo {
	public static void main(String[] args) {
		System.out.println("hello world".indexOf('o'));
	}
}

public class Demo2 {
	public static void main(String[] args) {
		System.out.println("hello world".indexOf('o',5));
	}
}

例3:
public class Demo3 {
	public static void main(String[] args) {
		System.out.println("hello world".substring(6));
	}
}

public class Demo4 {
	public static void main(String[] args) {
		System.out.println("hello world".substring(6,8));
	}
}

 
 
二:集合クラス
集合クラスはオブジェクトのセットを格納するために使用され、各オブジェクトは要素と呼ばれ、Vector、Enumeration、ArrayList、Collection、Iterator、Set、Listなどの集合クラスとインタフェースがよく使用されます.   1.VectorクラスとEnumerationインタフェースの例1:キーボードに入力されたデジタルシーケンスの各デジタルをVectorオブジェクトに格納し、画面に各デジタル加算の結果を印刷します.たとえば、1234を入力し、10を印刷します.
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

public class Demo {
	public static void main(String[] args) {
		int b=0;
		Vector v = new Vector();
		System.out.println(" :");
		while(true){
			try {
				b = System.in.read();
				if(b =='\r' || b =='
'){ break; }else{ int num = b-'0'; v.addElement(new Integer(num)); } } catch (IOException e) { e.printStackTrace(); } } int sum = 0; Enumeration e = v.elements(); while(e.hasMoreElements()){ Integer intObj = (Integer)e.nextElement(); sum +=intObj.intValue(); } System.out.println(sum); } }

   2.CollectionインタフェースとIteratorインタフェース:
例2:前の例を書き換える.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class Demo2 {
	public static void main(String[] args) {
		int b=0;
		ArrayList list = new ArrayList();
		System.out.println(" :");
		while(true){
			try {
				b = System.in.read();
				if(b =='\r' || b =='
'){ break; }else{ int num = b-'0'; list.add(new Integer(num)); } } catch (IOException e) { e.printStackTrace(); } } int sum = 0; Iterator e = list.iterator(); while(e.hasNext()){ Integer intObj = (Integer)e.next(); sum +=intObj.intValue(); } System.out.println(sum); } }

   3.Collection,Set,Listの違いは以下の通りである.
   (1).Collectionの各要素オブジェクト間には、指定された順序がなく、重複要素と複数のnull要素オブジェクトが許可されます.
   (2).Set各要素オブジェクト間には指定された順序がなく、重複要素は許可されず、null要素オブジェクトが最大1つ許可されます.
   (3).リストの各要素オブジェクト間には指定された順序があり、重複要素と複数のnull要素オブジェクトが許可されている.
例3:
import java.util.ArrayList;
import java.util.Collections;

public class Demo {
	public static void main(String[] args) {
		ArrayList a1 = new ArrayList();
		a1.add(new Integer(1));
		a1.add(new Integer(3));
		a1.add(new Integer(2));
		System.out.println(a1.toString());
		
		Collections.sort(a1);
		System.out.println(a1.toString());
	}
}