Java实现Notepad

Written by Bruth_Lee in Southwest University of Science And Technology.

You should learn about generic containter class,You may be clear about this aspect if you have read it patiently.

In this NoteBook,it can add something, remove something, get the index of you want contents of it and all of it.

package notepad;

import java.util.ArrayList;

public class NoteBook {
	
	private ArrayList notes = new ArrayList();
	
	public void add(String s) {
		notes.add(s);
	}
	
	public int getSize() {
		return notes.size();
	}
	
	public String getNote(int index) {
		return notes.get(index);
	}
	
	public void remove(int index) {
		notes.remove(index);
	}
	
	public String[] list() {
		String[] a = new String[notes.size()];
		notes.toArray(a);
		return a;
	}
	public static void main(String[] args) {
		String[] a = new String[2];
		a[0] = "first";
		a[1] = "second";
		NoteBook nb = new NoteBook();
		nb.add("first");
		nb.add("second");
		nb.add("third");
		System.out.println("The contents of NoteBook are : ");
		for(String s : a) {
			System.out.println(s);
		}
		System.out.print("The size of the NodeBook is : "+nb.getSize()+" bytes");
		System.out.println();
		nb.remove(2);
		System.out.println("The results as follow when I delete the third statements");
		for(String s : a) {
			System.out.println(s);
		}
	}	
}


你可能感兴趣的:(Java合集)