import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestArryList {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Hello");
list.add("World");
list.add("HaHaHa");
for (String str : list){
System.out.println(str);
}
String[] strArray = new String[list.size()];
list.toArray(strArray);
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
Iterator ite = list.listIterator();
while (ite.hasNext()){
System.out.println(ite.next());
}
}
}
运行结果:
常用方法:
1、add(Object element): 向列表的尾部添加指定的元素。
2、size(): 返回列表中的元素个数。
3、get(int index): 返回列表中指定位置的元素,index从0开始。
4、add(int index, Object element): 在列表的指定位置插入指定元素。
5、set(int i, Object element): 将索引i位置元素替换为元素element并返回被替换的元素。
6、clear(): 从列表中移除所有元素。
7、isEmpty(): 判断列表是否包含元素,不包含元素则返回 true,否则返回false。
8、contains(Object o): 如果列表包含指定的元素,则返回 true。
9、remove(int index): 移除列表中指定位置的元素,并返回被删元素。
10、remove(Object o): 移除集合中第一次出现的指定元素,移除成功返回true,否则返回false。
11、iterator(): 返回按适当顺序在列表的元素上进行迭代的迭代器。
Map 代码:package 集合;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestMap {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("1","value1");
map.put("2","value2");
map.put("3","value3");
System.out.println("通过Map.keySet便利key和value:");
for (String key :map.keySet()){
System.out.println("key="+key+"and value=" + map.get(key));
}
System.out.println("通过Map.entrySet使用iterator遍历key和value");
Iterator> it = map.entrySet().iterator();
while (it.hasNext()){
Map.Entry entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry entry : map.entrySet()){
System.out.println("key= " +entry.getKey() + " and value= " + entry.getValue());
}
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= "+v);
}
}
}
运行结果
常用方法
1、put(K key, V value): 向map集合中添加Key为key,Value为value的元素,当添加成功时返回null,否则返回value。
2、putAll(Map extends K,? extends V> m): 向map集合中添加指定集合的所有元素
3、clear(): 把map集合中所有的键值删除
4、containsKey(Object key): 检出map集合中有没有包含Key为key的元素,如果有则返回true,否则返回false。
5、containsValue(Object value):检出map集合中有没有包含Value为value的元素,如果有则返回true,否则返回false。
6、entrySet(): 返回map到一个Set集合中,以map集合中的Key=Value的形式返回到set中。
7、equals(Object o): 判断两个Set集合的元素是否相同
8、get(Object key): 根据map集合中元素的Key来获取相应元素的Value
9、hashCode(): 返回map集合的哈希码值
10、isEmpty():检出map集合中是否有元素,如果没有则返回true,如果有元素则返回false
11、keySet(): 返回map集合中所有Key
12、remove(Object key):删除Key为key值的元素
13、size():返回map集合中元素个数
14、values():返回map集合中所有的Value到一个Collection集合
多线程 案例一:实现Runnable接口 代码package 多线程;
public class RunnableDemo implements Runnable{
private Thread t;
private String threadName;
RunnableDemo(String name){
threadName = name;
System.out.println("Creating" + threadName);
}
public void run(){
System.out.println("Running" + threadName);
try {
for (int i = 4 ; i > 0; i--){
System.out.println("Thread:" + threadName + "," + i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread" + threadName + "interrupted.");
}
System.out.println("Thread" + threadName + "exiting");
}
public void start(){
System.out.println("Starting" + threadName);
if (t == null){
t = new Thread(this,threadName);
t.start();
}
}
}
package 多线程;
public class TestThread {
public static void main(String[] args) {
RunnableDemo R1 = new RunnableDemo("Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo("Thread-2");
R2.start();
}
}
运行结果
案例二:继承Thread类
package 多线程2;
public class ThreadDemo extends Thread{
private Thread t;
private String threadName;
ThreadDemo(String name){
threadName = name;
System.out.println("Creating" + threadName);
}
@Override
public void run() {
try {
for (int i = 0; i<4 ;i++){
System.out.println("Running" + threadName + "," + i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread" + threadName + "interrupted.");
}
System.out.println("Thread" + threadName + "exiting.");
}
@Override
public void start() {
System.out.println("Starting" + threadName);
if (t == null){
t = new Thread(this,threadName);
t.start();
}
}
}
package 多线程2;
public class TestThread {
public static void main(String[] args) {
ThreadDemo threadDemo1 = new ThreadDemo("Thread-1");
threadDemo1.start();
ThreadDemo threadDemo2 = new ThreadDemo("Thread-2");
threadDemo2.start();
}
}
运行结果
案例三:守护线程
代码
package 多线程3;
public class DisplayMessage implements Runnable{
private String message;
public DisplayMessage(String message) {
this.message = message;
}
@Override
public void run() {
while (true){
System.out.println(message);
}
}
}
package 多线程3;
public class GuessANumber extends Thread{
private int number;
public GuessANumber(int number) {
this.number = number;
}
public void run() {
int counter = 0;
int guess = 0;
do{
guess = (int)(Math.random() * 10 + 1);
System.out.println(this.getName() +" guesses " +guess);
counter++;
} while(guess != number);
System.out.println("** Correct!" + this.getName() +"in" + counter + "guesses.**");
}
}
package 多线程3;
public class ThreadClassDemo {
public static void main(String args[]) {
Runnable hello = new DisplayMessage("Hello");
Thread thread1 = new Thread(hello);
thread1.setDaemon(true);
thread1.setName("hello");
System.out.println("Starting hello thread...");
thread1.start();
Runnable bye = new DisplayMessage("Goodbye");
Thread thread2 = new Thread(bye);
thread2.setPriority(Thread.MIN_PRIORITY);
thread2.setDaemon(true);
System.out.println("Starting goodbye thread...");
thread2.start();
System.out.println("Starting thread3...");
Thread thread3 = new GuessANumber(27);
thread3.start();
try {
thread3.join();
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
System.out.println("Starting thread4...");
Thread thread4 = new GuessANumber(75);
thread4.start();
System.out.println("main() is ending...");
}
}
运行结果
当线程一工作
当线程二工作
当线程三工作
当线程四工作
总结多线程方法
1.sleep():
sleep()方法是将线程停止指定的时间,时间到了之后会继续执行。
2.wait() notify()wait()属于Object类,与sleep()的区别是当前线程会释放锁,进入等待此对象的等待锁定池。
3.join()在线程A执行过程中加入线程B,等待线程B执行完毕后返回继续执行线程A
4.yield()可以把自己的位置让给其他线程
5.setDaemon(true)将线程变成守护线程
面向对象面向对象就是将操作一件事情变得对象来操作更加注重的是对象来操作事情而不是注重过程,例如将大象放入冰箱,传统的面向过程就是将冰箱门打开,再将大象放入冰箱最后在关闭冰箱,而面向对象则是将大象和冰箱和我看作三个对象,然后我调用冰箱打开方法将冰箱打开,然后调用将大象放入冰箱方法将大象放入冰箱,最后我再调用冰箱的关闭方法。
封装将自身封装起来,对外界隐藏信息,同时也对外界提供方法,使外界可使用。
继承将对象和对象联系起来构建父子关系,子类可以调用或者重写父类方法继承父类的属性等,对于父类来说可以扩展更多的属性及方法。
多态子类对象可以与父类对象进行转换,而且根据其使用的子类不同完成的功能也不同,子类可变为父类(向上转型),父类可变为子类(向下转型)。



