java,
java,
通用
Eclipse怎样连接并打开oracle等数据库:http://jingyan.baidu.com/article/a501d80cea3ed4ec630f5e2f.html
包名中的字母一律小写,如:demo.java。
类名、接口名应当使用名词,每个单词的首字母大写,如:TestPerson。
方法名,第一个单词小写,后面每个单词的首字母大写,如:talkMySelf。
常量名中的每个字母一律大写,如:COUNTRY。
只要在命令行中用以下命令就可以将一个包打成一个jar 文件:
jar –cvf create.jar demo
在 Java 中声明类、属性和方法时,可使用关键字 final 来修饰。
1、 final 标记的类不能被继承。
2、 final 标记的方法不能被子类复写。
3、 final 标记的变量(成员变量或局部变量)即为常量,只能赋值一次。
instanceof 关键字判断 p 对象是否是 Student 的实例:
Person p = new Student();
// 判断对象 p 是否是 Student 类的实例
if (p instanceof Student) {
// 将 Person 类的对象 p 转型为 Student 类型
Student s = (Student) p;
s.fun1();
} else {
p.fun2();
}
4、数组
Person p[] = {
new Person("张三",25),new Person("李四",30),new Person("王五",35)
};
for(int i=0;i<p.length;i++)
{
System.out.println(p[i].talk()) ;
}
try
{要检查的程序语句}
catch(异常类 对象名称)
{异常发生时的处理语句 ;}
finally
{一定会运行到的程序代码}
public class test {
public static void main(String args[])
{
try
{
int arr[]=new int[5];
arr[10]=7;
if (b == 0)throw new ArithmeticException("一个算术异常"); // 抛出异常
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组超出绑定范围!");
System.out.println("异常:"+e); // 显示异常对象 e 的内容
}
System.out.println("main()方法结束!");
}
}
检查环境变量getProperties
getProperties 方法是获得当前虚拟机的环境属性,增加java环境变量(Author=lxh)java -DAuthor=lxh SystemInfo;
import java.util.*;
public class test2 {
public static void main(String[] args) {
Properties sp = System.getProperties();
Enumeration e = sp.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + " = " + sp.getProperty(key));
}
}
}
类封装
记住:“==”是比较内存地址值的,“equals”是比较内容的。
class Person// 封装的公共类
{/*public:表示公共方法
static:表示此方法为一静态方法,可以由类名直接调用
void:表示此方法无返回值
main:系统定义的方法名称
String args[]:接收运行时参数*/
String name;
int age;
void talk() {
System.out.println("我是:" + name + ",今年:" + age + "岁");
}
static
{
System.out.println("Person 类的静态代码块被调用!");//只被调用一次
}
}
public class test {
public static void main(String[] args) {
Person p1 = new Person();//调用公共类
p1.name = "张三";
p1.age = 25;
p1.talk();
}
}
类封装2
class Person2// 封装的公共类
{
private String name;
private int age;
void talk() {
System.out.println("我是:" + name + ",今年:" + age + "岁");
}
public void setName(String str) {//接收参数
name = str;
}
public String getName() {
return name;
}
}
public class test {
public static void main(String[] args) {
Person2 p2 = new Person2();//调用公共类
p2.setName("张三2");//参数"张三2"
System.out.print(p2.getName());
p2.talk();
}
}
类封装3
class Change {
int x = 0;
}
public class test{
public static void main(String[] args) {
Change c = new Change();
c.x = 20;
fun(c);
System.out.println("x = " + c.x);
}
public static void fun(Change c1) {
c1.x = 25;
}
}
类封装4 p1.compare(p2)
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
boolean compare(Person p) {//比较方法
if (this.name.equals(p.name) && this.age == p.age) {
return true;
} else {
return false;
}
}
}
public class test {
public static void main(String[] args) {
Person p1 = new Person("张三", 30);
Person p2 = new Person("张三", 30);
System.out.println(p1.compare(p2) ? "相等,是同一人!" : "不相等,不是同一人!");
}
}
静态方法 private static String
class Person {
String name;
private static String city = "中国";
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String talk() {
return "我是:" + this.name + ",今年:" + this.age + "岁,来自:" + city;
}
public static void setCity(String c) {
city = c;
}
}
public class test {
public static void main(String[] args) {
Person p1 = new Person("张三", 25);
Person p2 = new Person("李四", 30);
Person p3 = new Person("王五", 35);
System.out.println("修改之前信息:" + p1.talk());
System.out.println("修改之前信息:" + p2.talk());
System.out.println("修改之前信息:" + p3.talk());
System.out.println(" **************修改之后信息**************");
// 修改后的信息
Person.setCity("美国");
System.out.println("修改之后信息:" + p1.talk());
System.out.println("修改之后信息:" + p2.talk());
System.out.println("修改之后信息:" + p3.talk());
}
}
类封装5 内部实例化
final 关键字,此关键字表示 Person 的对象 p 不能被重新实例化
class Person {
String name;
private static final Person p = new Person();
private Person() {
name = "张三";
}
public static Person getP() {
return p;
}
}
public class test {
public static void main(String[] args) {
// 声明一 Person 类的对象
Person p = null;
p = Person.getP();
System.out.println(p.name);
}
}
类封装6 内部类
外部类是无法找到内部类中所声明的属性。而内部类则可以访问外部类的属性。
class Outer {
int score = 95;
void inst() {
Inner in = new Inner();
in.display();
}
class Inner {//public class Inner
void display() {
System.out.println("成绩: score = " + score);
}
}
}
public class test {
public static void main(String[] args) {
Outer outer = new Outer();
outer.inst();
/*public class Inner设置了public后可以引用内部类方法
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display() ;*/
}
}
类封装7 方法内部类
外部类是无法找到内部类中所声明的属性。而内部类则可以访问外部类的属性。
class Outer {
int score = 95;
void inst() {
class Inner {
void display() {
System.out.println("成绩: score = " + score);
}
}
Inner in = new Inner();
in.display();
}
}
public class test {
public static void main(String[] args) {
Outer outer = new Outer();
outer.inst();
}
}
2:
class Outer {
int score = 95;
void inst(final int s) {
final int temp = 20;
class Inner {
void display() {
System.out.println("成绩: score = " + (score + s + temp));
}
}
Inner in = new Inner();
in.display();
}
}
public class test {
public static void main(String[] args) {
Outer outer = new Outer();
outer.inst(5);
}
}
类的继承extends
super 主要的功能是完成子类调用父类中的内容,
例子:
class Person {
String name;
int age;
}
class Student extends Person {
String school;
}
public class test {
public static void main(String[] args) {
Student s = new Student();
}
}
例子2:构造方法
class Person {
String name;
int age;
// 增加一个什么都不做的无参构造方法
public Person() {}//去掉会报错
// 父类的构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Student extends Person {
String school;
// 子类的构造方法
public Student() {
}
}
public class test {
public static void main(String[] args) {
Student s = new Student();
}
}
类的继承extends
super 主要的功能是完成子类调用父类中的内容,
例子:
class Person {
String name;
int age;
}
class Student extends Person {
String school;
}
public class test {
public static void main(String[] args) {
Student s = new Student();
}
}
例子2:构造方法
class Person {
String name;
int age;
// 增加一个什么都不做的无参构造方法
public Person() {}//去掉会报错
// 父类的构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Student extends Person {
String school;
// 子类的构造方法
public Student() {
}
}
public class test {
public static void main(String[] args) {
Student s = new Student();
}
}
例子3调用父类的构造方法:
class Person {
String name;
int age;
// 父类的构造方法
public Person() {
}
public String talk() {
return "我是:" + this.name + ",今年:" + this.age + "岁";
}
}
class Student extends Person {
String school;
// 子类的构造方法
public Student(String name, int age, String school) {
// 在这里用 super 调用父类中的属性
super.name = name;
super.age = age;
// 调用父类中的 talk()方法
System.out.print(super.talk());
// 调用本类中的 school 属性
this.school = school;
}
}
public class test {
public static void main(String[] args) {
Student s = new Student("张三", 25, "北京");
System.out.println(",学校:" + s.school);
}
}
子类复写重载
super 主要的功能是完成子类调用父类中的内容,
class Person {
String name;
int age;
public String talk() {
return "我是:" + this.name + ",今年:" + this.age + "岁";
}
}
class Student extends Person {
String school;
public Student(String name, int age, String school) {
// 分别为属性赋值
this.name = name;
this.age = age;
this.school = school;
}
// 此处复写 Person 类中的 talk()方法
public String talk() {
return super.talk() + ",我在" + this.school + "上学";
}
}
class test {
public static void main(String[] args) {
Student s = new Student("张三", 25, "北京");
// 此时调用的是子类中的 talk()方法
System.out.println(s.talk());
}
}
抽象类abstract
super 主要的功能是完成子类调用父类中的内容,
abstract class Person {
String name;
int age;
String occupation;
// 声明一抽象方法 talk()
public abstract String talk();//抽象方法,留给子类去实现
}
// Student 类继承自 Person 类
class Student extends Person {
public Student(String name, int age, String occupation) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
// 复写 talk()方法
public String talk()
{
return "学生——>姓名:"+this.name+",年龄:"+this.age+",职业:"+this.occupation+"!" ;
}
}
// Worker 类继承自 Person 类
class Worker extends Person {
public Worker(String name, int age, String occupation) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
// 复写 talk()方法
public String talk()
{
return "工人——>姓名:"+this.name+",年龄:"+this.age+",职业:"+this.occupation+"!" ;
}
}
class test {
public static void main(String[] args) {
Student s = new Student("张三", 20, "学生");
Worker w = new Worker("李四", 30, "工人");
System.out.println(s.talk());
System.out.println(w.talk());
}
}
接口interface
(1)、接口里的“抽象方法”只要做声明即可,而不用定义其处理的方式;(2)、数据成员必须赋初值;接口是 java 实现多继承的一种机制,一个类只能继承一个父类,但如果需要一个类继承多个抽象方法的话,就明显无法实现,所以就出现了接口的概念。一个类只可以继承一个父类,但却可以实现多个接口。
interface Person {
String name = "张三";
int age = 25;
String occupation = "学生";
// 声明一抽象方法 talk()
public abstract String talk();
}
// Student 类继承自 Person 类
class Student implements Person {
// 复写 talk()方法
public String talk() {
return "学生——>姓名:" + this.name + ",年龄:" + this.age + ",职业:" + this.occupation + "!";
}
}
class test {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.talk());
}
}
接口的继承implements
interface A {
int i = 10;
public void sayI();
}
interface E {
int x = 40;
public void sayE();
}
// B 同时继承了 A、E 两个接口
interface B extends A, E {
int j = 20;
public void sayJ();
}
// C 继承实现 B 接口,也就意味着要实现 A、B、E 三个接口的抽象方法
class C implements B {
public void sayI() {
System.out.println("i = " + i);
}
public void sayJ() {
System.out.println("j = " + j);
}
public void sayE() {
System.out.println("e = " + x);
}
}
class test {
public static void main(String[] args) {
C c = new C();
c.sayI();
c.sayJ();
c.sayE();
}
}
接口实例化
接口是无法直接实例化的,因为接口中没有构造方法,但是却可以根据对象多态性的概念,通过接口的子类对其进行实例化
interface Person {
public void fun1();
}
class Student implements Person {
public void fun1() {
System.out.println("Student fun1()");
}
}
class test {
public static void main(String[] args) {
Person p = new Student();
p.fun1();
}
}
接口对象的实例化(关注)
interface Usb {
public void start();
public void stop();
}
class MoveDisk implements Usb {
public void start() {
System.out.println("MoveDisk start...");
}
public void stop() {
System.out.println("MoveDisk stop...");
}
}
class Mp3 implements Usb {
public void start() {
System.out.println("Mp3 start...");
}
public void stop() {
System.out.println("Mp3 stop...");
}
}
class Computer {
public void work(Usb u) {
u.start();
u.stop();
}
}
class test {
public static void main(String[] args) {
new Computer().work(new MoveDisk());
new Computer().work(new Mp3());
}
}
匿名内部类
interface A {
public void fun1();
}
/*class C implements A {
public void fun1() {
System.out.println(i);
}
}*/被匿名类
class B {
int i = 10;
public void get(A a) {
a.fun1();
}
public void test() {
this.get(new A() {//原this.get(new C());匿名了
public void fun1() {
System.out.println(i);
}
});
}
}
class test {
public static void main(String[] args) {
B b = new B();
b.test();
}
}
编写异常类
class DefaultException extends Exception {//此类继承自 Exception 类
public DefaultException(String msg) {
// 调用 Exception 类的构造方法,存入异常信息
super(msg);
}
}
public class test {
public static void main(String[] args) {
try {
// 在这里用 throw 直接抛出一个 DefaultException 类的实例对象
throw new DefaultException("自定义异常!");
} catch (Exception e) {
System.out.println(e);
}
}
}
类公开import
package demo.java.a ;
public class Person//public 公开类
{
public String talk()
}
package demo.java.b ;
import demo.java.a.Person ;//导入类
class TestPackage2
{
public static void main(String[] args)
{
System.out.println(new Person().talk()) ;//使用类
}
}
或
package demo.java.b ;
class TestPackage3
{
public static void main(String[] args)
{
System.out.println(new demo.java.a.Person().talk());//使用类
}
}
多线程extends Thread.start()
任何线程一般具有五种状态,即创建、就绪、运行、阻塞、终止;如果调用 sleep()、suspend()、wait()等方法,线程都将进入堵塞状态。堵塞时,线程不能进入排队队列,只有当引起堵塞的原因被消除后,线程才可以转入就绪状态。
线程调用 stop()方法时或 run()方法执行结束后,线程即处于死亡状态。处于死亡状态的线程不具有继续运行的能力
public class test {
public static void main(String args[]) {
new TestThread().start();
// 循环输出
for (int i = 0; i < 10; i++) {
System.out.println("main 线程在运行");
}
}
}
class TestThread extends Thread{
public void run() {
for (int i = 0; i < 10; i++)
System.out.println("TestThread 在运行");
}
}
//例子2:各自占有各自的资源(tickets = 20),用 Thread 类实际上无法达到资源共享的目的
public class test {
public static void main(String args[]) {
/*TestThread t=new TestThread();
t.start();*/
// 一个线程对象只能启动一次
// 启动了2个线程,分别执行各自的操作
new TestThread().start();
new TestThread().start();
}
}
class TestThread extends Thread {//Thread
private int tickets = 20;
public void run() {
while (true) {
if (tickets > 0)
System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
}
}
}
例子3启动了四个线程,并实现了资源共享的目的:
public class test implements Runnable {
private int tickets = 100;
public void run() {
while (true) {
if (tickets > 0)
System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
}
}
public static void main(String args[]) {
test t = new test();
new Thread(t).start();
new Thread(t).start();
}
}
例子2:
public class test implements Runnable {
private int tickets = 100;
public void run() {
while (true) {
if (tickets > 0)
System.out.println("售票员:'"+Thread.currentThread().getName() + "'出售票" + tickets--);
}
}
public static void main(String args[]) {
test t = new test();
Thread tt = new Thread(t);
tt.setName("小张");//线程改名
tt.start();
Thread bb = new Thread(t);
bb.setName("小王");
bb.start();
}
}
例子3:
public class test implements Runnable {
public Integer tickets;
public test(int tickets) {
this.tickets = tickets;
}
@Override
public void run() {
synchronized (tickets) {
while (tickets > 0) {
while (tickets > 20) {
System.out.println(Thread.currentThread().getName() + ":" + tickets--);
}
if (Thread.currentThread().getName().equals("B")) {
System.out.println(Thread.currentThread().getName() + ":" + tickets-- + "\t");
}
}
}
}
public static void main(String[] args) throws InterruptedException {
test ticket = new test(100);
new Thread(ticket, "A").start();
new Thread(ticket, "B").start();
}
}
多线程implements Runnable
如果一个类继承了某一个类,同时又想采用多线程技术的时,就不能用 Thread 类产生线程
public class test {
public static void main(String args[]) {
TestThread t = new TestThread() ;
new Thread(t).start();
// 循环输出
for (int i = 0; i < 10; i++) {
System.out.println("main 线程在运行");
}
}
}
class TestThread implements Runnable{
public void run() {
for (int i = 0; i < 10; i++)
System.out.println("TestThread 在运行");
}
}
后台线程setDaemon(true)
如果某个线程对象在启动(调用 start()方法)之前调用了 setDaemon(true)方法,这个线程就变成了后台线程,进程中只有后台线程运行时,进程就会结束的说法。
public class test {
public static void main(String args[]) {
ThreadTest t = new ThreadTest();
Thread tt = new Thread(t);
tt.setDaemon(true); // 设置后台运行
tt.start();
}
}
class ThreadTest implements Runnable {
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + "is running.");
}
}
}
线程同步synchronized,会小概率产生死锁
如果一个类继承了某一个类,同时又想采用多线程技术的时,就不能用 Thread 类产生线程
public class test {
public static void main(String[] args) {
TestThread t = new TestThread();
// 启动了四个线程,实现了资源共享的目的
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class TestThread implements Runnable {
private int tickets = 20;
public void run() {
while (true) {
synchronized (this) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
}
}
}
}
}
解决资源不同步(set get)
如果一个类继承了某一个类,同时又想采用多线程技术的时,就不能用 Thread 类产生线程
class Producer implements Runnable {
P q = null;
public Producer(P q) {
this.q = q;
}
public void run() {
int i = 0;
while (true) {
if (i == 0) {
/*q.name = "张三";
q.sex = "男";*/
q.set("张三","男");
} else {
/*q.name = "李四";
q.sex = "女";*/
q.set("李四","女");
}
i = (i + 1) % 2;
}
}
}
class P {
/*String name = "李四";
String sex = "女";*/
private String name="李四";
private String sex="女";
public synchronized void set(String name,String sex)
{
this.name = name ;
this.sex =sex ;
}
public synchronized void get()
{
System.out.println(this.name + " ---->" + this.sex ) ;
}
}
class Consumer implements Runnable {
P q = null;
public Consumer(P q) {
this.q = q;
}
public void run() {
while (true) {
q.get();
}
}
}
public class test {
public static void main(String[] args) {
P q = new P();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();
}
}
线程间通讯
wait、notify、notifyAll 这三个方法只能在 synchronized 方法中调用,即无论线程调用一个对象的 wait 还是 notify 方法,该线程必须先得到该对象的锁标记,这样,notify只能唤醒同一对象监视器中调用 wait 的线程,使用多个对象监视器,就可以分别有多个 wait、notify 的情况,同组里的 wait 只能被同组的 notify 唤醒
class Producer implements Runnable {
P q = null;
public Producer(P q) {
this.q = q;
}
public void run() {
int i = 0;
while (true) {
if (i == 0) {
q.set("张三", "男");
} else {
q.set("李四", "女");
}
i = (i + 1) % 2;
}
}
}
class P {
private String name = "李四";
private String sex = "女";
boolean bFull = false;
public synchronized void set(String name, String sex) {
if (bFull) {
try {
wait(); // 后来的线程要等待
} catch (InterruptedException e) {
}
}
this.name = name;
try {
Thread.sleep(10);
} catch (Exception e) {
System.out.println(e.getMessage());
}
this.sex = sex;
bFull = true;
notify();// 唤醒最先到达的线程
}
public synchronized void get() {
if (!bFull) {
try {
wait();//等待
} catch (InterruptedException e) {
}
}
System.out.println(name + " ---->" + sex);
bFull = false;
notify();//唤醒
}
}
class Consumer implements Runnable {
P q = null;
public Consumer(P q) {
this.q = q;
}
public void run() {
while (true) {
q.get();
}
}
}
public class test {
public static void main(String[] args) {
P q = new P();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();
}
}
合并流(文件内容合并)-383
import java.io.*;
public class test {
public static void main(String[] args) throws IOException {
// 声明两个文件读入流
FileInputStream in1 = null, in2 = null;
// 声明一个序列流
SequenceInputStream s = null;
FileOutputStream out = null;
try {
// 构造两个被读入的文件
File inputFile1 = new File("d:\\1.txt");
File inputFile2 = new File("d:\\2.txt");
// 构造一个输出文件
File outputFile = new File("d:\\12.txt");
in1 = new FileInputStream(inputFile1);
in2 = new FileInputStream(inputFile2);
// 将两输入流合为一个输入流
s = new SequenceInputStream(in1, in2);
out = new FileOutputStream(outputFile);
int c;
while ((c = s.read()) != -1)
out.write(c);
in1.close();
in2.close();
s.close();
out.close();
System.out.println("ok...");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in1 != null)
try {
in1.close();
} catch (IOException e) {
}
if (in2 != null)
try {
in2.close();
} catch (IOException e) {
}
if (s != null)
try {
s.close();
} catch (IOException e) {
}
if (out != null)
try {
out.close();
} catch (IOException e) {
}
}
}
}
txt内容写入Properties 类 store() load()
import java.io.* ;
import java.util.* ;
public class test2
{
public static void main(String[] args)
{
Properties settings = new Properties();
try {
settings.load(new FileInputStream("c:\\count.txt"));
} catch (Exception e) {
settings.setProperty("count", new Integer(0).toString());
}
int c = Integer.parseInt(settings.getProperty("count")) + 1;
System.out.println("这是本程序第" + c + "次被使用");
settings.put("count", new Integer(c).toString());
try {
settings.store(new FileOutputStream("d:\\1.txt"), "PropertiesFile use it .");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
执行其他程序Runtime-exec
public class test2
{
public static void main(String[] args)
{
Runtime run = Runtime.getRuntime() ;
try
{
run.exec("notepad.exe") ;//执行其它程序时使用 exec 方法
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
时间日期Calendar data
import java.util.*;
public class test2 {
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
// 下面打印当前时间
System.out.println(c1.get(c1.YEAR) + "年" + (c1.get(c1.MONTH) + 1) + "月" + c1.get(c1.DAY_OF_MONTH) + "日"
+ c1.get(c1.HOUR) + ":" + c1.get(c1.MINUTE) + ":" + c1.get(c1.SECOND));
// 增加天数为 230
c1.add(c1.DAY_OF_YEAR, 230);
// 下面打印的是 230 天后的时间
System.out.println(c1.get(c1.YEAR) + "年" + (c1.get(c1.MONTH) + 1) + "月" + c1.get(c1.DAY_OF_MONTH) + "日"
+ c1.get(c1.HOUR) + ":" + c1.get(c1.MINUTE) + ":" + c1.get(c1.SECOND));
}
}
产生随机数Random
import java.util.*;
public class test2 {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 5; i++) {
System.out.print(r.nextInt(100) + "\t");
}
}
}
Collection 方法 描述
boolean add(Object obj) 将obj加入到调用类集中。如果obj被加入到类集中了,则返回
true;如果obj已经是类集中的一个成员或类集不能被复制时,
则返回false
boolean addAll(Collection c) 将c中的所有元素都加入到调用类集中,如果操作成功(元素被
加入了),则返回true;否则返回false
void clear( ) 从调用类集中删除所有元素
boolean contains(Object obj) 如果obj是调用类集的一个元素,则返回true,否则,返回false
boolean containsAll(Collection
c) 如果调用类集包含了c中的所有元素,则返回true;否则,返回
false
boolean equals(Object obj) 如果调用类集与obj相等,则返回true;否则返回false
int hashCode( ) 返回调用类集的散列码
boolean isEmpty( ) 如果调用类集是空的,则返回true;否则返回false
Iterator iterator( ) 返回调用类集的迭代程序
Boolean remove(Object obj) 从调用类集中删除obj的一个实例。如果这个元素被删除了,则
返回true;否则返回false
Boolean removeAll(Collection
c) 从调用类集中删除c的所有元素。如果类集被改变了(也就是说
元素被删除了),则返回true;否则返回false
Boolean retainAll(Collection
c) 删除调用类集中除了包含在c中的元素之外的全部元素。如果类
集被改变了(也就是说元素被删除了),则返回true,否则返
回false
int size( ) 返回调用类集中元素的个数
Object[ ] toArray( ) 返回一个数组,该数组包含了所有存储在调用类集中的元素。
数组元素是类集元素的拷贝
Object[ ] toArray(Object
array[ ]) 返回一个数组,该数组仅仅包含了那些类型与数组元素类型匹
配的类集元素。数组元素是类集元素的拷贝。如果array的大小
与匹配元素的个数相等,它们被返回到array。如果array的大
小比匹配元素的个数小,将分配并返回一个所需大小的新数组,
如果array的大小比匹配元素的个数大,在数组中,在类集元素
之后的单元被置为null。如果任一类集元素的类型都不是array
的子类型,则引发一个ArrayStoreException异常
List 方法 描述
方法 描述
Void add(int index,Object obj) 将obj插入调用列表,插入位置的下标由index
传递。任何已存在的,在插入点以及插入点之
后的元素将前移。因此,没有元素被覆盖
Boolean addAll(int
index,Collection c) 将c中的所有元素插入到调用列表中,插入点的
下标由index传递。在插入点以及插入点之后的
元素将前移。因此,没有元素被覆盖。如果调
用列表改变了,则返回true;否则返回false
Object get(int index) 返回存储在调用类集内指定下标处的对象
int indexOf(Object obj) 返回调用列表中obj的第一个实例的下标。如果
obj不是列表中的元素,则返回-1
int lastIndexOf(Object obj) 返回调用列表中obj的最后一个实例的下标。如
果obj不是列表中的元素,则返回-1
ListIterator listIterator() 返回调用列表开始的迭代程序
ListIterator listIterator(int
index) 返回调用列表在指定下标处开始的迭代程序
Object remove(int index) 删除调用列表中index位置的元素并返回删除
的元素。删除后,列表被压缩。也就是说,被
删除元素后面的元素的下标减一
Object set(int index,Object obj) 用obj对调用列表内由index指定的位置进行赋
值
List subList(int start,int end) 返回一个列表,该列表包括了调用列表中从
start到end–1的元素。返回列表中的元素也被
调用对象引用
SortedSet 方法排序
方法 描述
Comparator comparator( ) 返回调用被排序集合的比较函数,如果对该
集合使用自然顺序,则返回null
Object first( ) 返回调用被排序集合的第一个元素
SortedSet headSet(Object end) 返 回 一 个 包 含 那 些 小 于 end 的 元 素 的
SortedSet,那些元素包含在调用被排序集合
中。返回被排序集合中的元素也被调用被排
序集合所引用
Object last( ) 返回调用被排序集合的最后一个元素
SortedSet subSet(Object start,
Object end) 返回一个SortedSet,它包括了从start到
end–1的元素。返回类集中的元素也被调用
对象所引用
SortedSet tailSet(Object start) 返回一个SortedSet,它包含了那些包含在分
类集合中的大于等于start的元素。返回集合
中的元素也被调用对象所引用
数组处理
import java.util.*;
public class test2 {
public static void main(String args[]) {
// 创建一个 ArrarList 对象
ArrayList al = new ArrayList();
System.out.println("a1 的初始化大小:" + al.size());
// 向 ArrayList 对象中添加新内容
al.add("C"); // 0 位置
al.add("A"); // 1 位置
al.add("E"); // 2 位置
al.add("B"); // 3 位置
al.add("D"); // 4 位置
al.add("F"); // 5 位置
// 把 A2 加在 ArrayList 对象的第 2 个位置
al.add(1, "A2"); // 加入之后的内容:C A2 A E B D F
System.out.println("a1 加入元素之后的大小:" + al.size());
// 显示 Arraylist 数据
System.out.println("a1 的内容: " + al);
// 从 ArrayList 中移除数据
al.remove("F");
al.remove(2); // C A2 E B D
System.out.println("a1 删除元素之后的大小: " + al.size());
System.out.println("a1 的内容: " + al);
}
}
public class test2 {
public static void main(String args[]) {
// 创建 LinkedList 对象
LinkedList ll = new LinkedList();
// 加入元素到 LinkedList 中
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
// 在链表的最后个位置加上数据
ll.addLast("Z");
// 在链表的第一个位置上加入数据
ll.addFirst("A");
// 在链表第二个元素的位置上加入数据
ll.add(1, "A2");
System.out.println("ll 最初的内容:" + ll);
// 从 linkedlist 中移除元素
ll.remove("F");
ll.remove(2);
System.out.println("从 ll 中移除内容之后:" + ll);
// 移除第一个和最后一个元素
ll.removeFirst();
ll.removeLast();
System.out.println("ll 移除第一个和最后一个元素之后的内容:" + ll);
// 取得并设置值
Object val = ll.get(2);
ll.set(2, (String) val + " Changed");
System.out.println("ll 被改变之后:" + ll);
}
}
按顺序存储:
public class test2 {
public static void main(String args[]) {
// 创建一 TreeSet 对象
TreeSet ts = new TreeSet();
// 加入元素到 TreeSet 中
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
System.out.println(ts);
}
}
[A, B, C, D, E, F]
数组迭代方法Vector :
public class test2 {
public static void main(String args[]) {
// 创建一个 ArrayList 数组
ArrayList al = new ArrayList();
// 加入元素到 ArrayList 中
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
// 使用 iterator 显示 a1 中的内容
System.out.print("a1 中原始内容是:");
Iterator itr = al.iterator();
while (itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// 在 ListIterator 中修改内容
ListIterator litr = al.listIterator();
while (litr.hasNext())
{
Object element = litr.next();
// 用 set 方法修改其内容
litr.set(element + "+");
}
System.out.print("a1 被修改之后的内容:");
itr = al.iterator();
while (itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// 下面是将列表中的内容反向输出
System.out.print("将列表反向输出:");
// hasPreviours 由后向前输出
while (litr.hasPrevious()) {
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}
数组处理
方法 描述
boolean hasNext( ) 如果存在更多的元素,则返回true,否则返回false
Object next( ) 返回下一个元素。如果没有下一个元素,则引发
NoSuchElementException异常
void remove( ) 删除当前元素,如果试图在调用next( )方法之后,调用
remove( )方法,则引发IllegalStateException异常
方法 描述
void add(Object obj) 将obj插入列表中的一个元素之前,该元素在下一次调
用next( )方法时,被返回
boolean hasNext( ) 如果存在下一个元素,则返回true;否则返回false
boolean hasPrevious( ) 如果存在前一个元素,则返回true;否则返回false
Object next( ) 返回下一个元素,如果不存在下一个元素,则引发一个
NoSuchElementException异常
int nextIndex( ) 返回下一个元素的下标,如果不存在下一个元素,则返
回列表的大小
Object previous( ) 返回前一个元素,如果前一个元素不存在,则引发一个
NoSuchElementException异常
int previousIndex( ) 返回前一个元素的下标,如果前一个元素不存在,则返
回-1
void remove( ) 从列表中删除当前元素。如果remove( )方法在next( )
方法或previous( )方法调用之前被调用,则引发一个
IllegalStateException异常
void set(Object obj) 将obj赋给当前元素。这是上一次调用next( )方法或
previous( )方法最后返回的元素
栈
方法 描述
boolean empty() 如果堆栈是空的,则返回true,当堆栈包含有元素时,
返回false
Object peek() 返回位于栈顶的元素,但是并不在堆栈中删除它
Object pop() 返回位于栈顶的元素,并在进程中删除它
Object push(Object
element) 将element压入堆栈,同时也返回element
int search(Object element) 在堆栈中搜索element,如果发现了,则返回它相对于
栈顶的偏移量。否则,返回-1调用push()方法可将一
个对象压入栈顶。调用pop()方法可以删除和返回栈顶
的元素。当调用堆栈是空的时,如果调用pop()方法,
将引发一个EmptyStackException异常。调用peek()
方法返回但不删除栈顶的对象。调用empty()方法,当
堆栈中没有元素时,返回true。
search() 方法确定一个对象是否存在于堆栈,并且返回将其指
向栈顶所需的弹出次数。
import java.util.*;
public class test2 {
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("入栈(" + a + ")");
System.out.println("Stack: " + st);
}
static void showpop(Stack st) {
System.out.print("出栈 -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("Stack: " + st);
}
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("Stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
// 出栈时会有一个 EmptyStackException 的异常,需要进行异常处理
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("出现异常:栈中内容为空");
}
}
}
数组处理
2、 TCP 程序的实现可用:Socket 类、ServerSocket 类。
3、 UDP 程序的实现可用:DatagramPacket 类、DatagramSocket 类。
相关文章
- 暂无相关文章
用户点评