android-nuc / 15_java_train Goto Github PK
View Code? Open in Web Editor NEW15级Android实验室培训
15级Android实验室培训
//作业一删除羊:
ArrayList sheeplist=new ArrayList();
sheeplist.add(new sheep("aa"));
sheeplist.add(new sheep("bb"));
sheeplist.add(new sheep("cc"));
sheeplist.add(new sheep("dd"));
sheeplist.add(new sheep("ee"));
//方法一:
for (int i = 0; i <=sheeplist.size();i++) {
sheeplist.remove(0);
}
//方法二:
for(int i=5;i>2;i--)
sheeplist.remove(sheeplist.get(i));
//循环:
for (int j = 0; j < sheeplist.size(); j++) {
System.out.println(((sheep)sheeplist.get(j)).getName());
}
//作业2:
public class main {
public static void main(String[] args) throws IOException{
File file=new File("D:\helloworld.txt");
File filebox=new File("d:\test");
if(!filebox.exists())
filebox.mkdir();
for (int j = 0; j < 10; j++) {
file=new File(filebox,"helloworld"+j+".txt");
try {
if(!file.exists())
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s="zzzz"+j;
OutputStream out=new FileOutputStream(file);
out.write(s.getBytes());
}
InputStream in=new FileInputStream(file);
byte[] b=new byte[5];
in.read(b);
String s2=new String(b);
System.out.println(s2);
//作业3:
class ExThread extends Thread{
public ExThread(String name){
super(name);
}
public ExThread(){
super();
}
public void run() {
for(int i=0;i<5;i++){
System.out.println("ExThread"+this.getName()+i);
}
}
}
class Threadi implements Runnable{
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void run() {
for(int i=0;i<5;i++){
System.out.println("runable"+this.getName()+i);
}
}
}
1.删除后三只羊
int num = sheepList.size();
System.out.println("从后向前删:");
for(int i = num - 1;i >= num - 3;i --){
sheepList.remove(i);
}
for(Object s:sheepList){
System.out.println(((Sheep)s).name);
}
System.out.println("从前向后删");
for(int i = 0;i < num - 3;i ++){
sheepList.remove(0);
}for(Object s:sheepList){
System.out.println(((Sheep)s).name);
}
System.out.println("foreach:");
int i = 0;
for(Object s:sheepList){
if(i >= 3){
sheepList.remove(((Sheep)s).name);
}else{
System.out.println(((Sheep)s).name);
}
i ++;
}
Iterator it = sheepList.iterator();
System.out.println("通过迭代器遍历:");
for(int i = num - 1;i > num - 3;i --){
if(i <= 3){
sheepList.remove(i);
}
while(it.hasNext()){
Sheep csn = (Sheep)it.next();
System.out.println(csn.name);
}
}
2.自动写文件
public static void main(String[] args) throws IOException{
//创建目标文件
File Demo = new File("e://YQ");
if(!Demo.exists()){
Demo.mkdir();
}
for(int i = 0;i < 10;i ++){
File file = new File("e://YQ//yq" + i + ".txt");
if(!file.exists()){
file.createNewFile();
}
}
String[] filenames = Demo.list();
for(int i = 0;i < filenames.length;i ++){
FileOutputStream fos = new FileOutputStream("e://YQ//yq" + i + ".txt");
String s = "我真" + i;
fos.write(s.getBytes());
fos.close();
System.out.println("第" + i + "次写入完成");
}
}
3.线程实现方法继承Thread与接口Runnnale的区别
public static void main(String[] args) {
//数据独立,其数值分别为3,2,1
Threadexend person1 = new Threadexend("关公");
Threadexend person2 = new Threadexend("秦琼");
person1.start();
person2.start();
//对象唯一,数据共享,数值一共是3,2,1
IThread person3 = new IThread();
new Thread(person3,"奥特曼").start();
new Thread(person3,"小怪兽").start();
}
static class Threadexend extends Thread{
public Threadexend(String name){
super(name);
}
public Threadexend(){
super();
}
private int nuqi = 3;
public void run(){
for(int i = 1;i <= 3;i ++){
if(nuqi > 0){
System.out.println(getName() +"第" + i + "次打了对方" + "," + "剩余怒气值" + nuqi--);
}
}
}
*_作业1 (删羊)_
int length = sheepList.size();
int n = 3;
//方法1:从前向后删n只羊
for(int i=0;i<n;i++){
sheepList.remove(i);
}
//方法2:从后向前删n只羊
for(int i=length;i>length-n-1;i--){
sheepList.remove(i);
}
//方法3:使用迭代器删除后n只羊
int count = 0;
Iterator<Sheep> itr = sheepList.iterator();
while(itr.hasNext()){
if(count >= length - n){
Sheep ed = (Sheep)itr.next();
itr.remove();
}else{
itr.next();
count++;
}
}
//方法4:使用迭代器删除指定的3只羊
Iterator<Sheep> itr = sheepList.iterator();
while(itr.hasNext()){
Sheep sp = (Sheep)itr.next();
if(sp.getName()=="懒羊羊"||sp.getName()=="慢羊羊"||sp.getName()=="沸羊羊")
itr.remove();
}
//方法5直接删除同一个索引3次即可删除3只羊(注意不可超过最大数)
sheepList.remove(2); //这里删的为第3只羊,删3次
sheepList.remove(2);
sheepList.remove(2);
***_作业2 (自动写文件)_
for(int i=0;i<9;i++){ //设置自动生成10个文件(0-9)
file=new File(manyfile,"helloworld"+i+".txt");//manyfile为文件路径
OutputStream out=null;
try {
out = new FileOutputStream(file);
String s = "我是"+i; //写入文件的内容
try {
out.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
作业3 (继承Thread类和实现runnable接口的比较)*******
public class MyThread extends Thread{
public static void main(String[] args) {
Thread th1 = new ThreadExtend("Thread***1***");
Thread th2 = new ThreadExtend("Thread***2***");
Thread th3 = new ThreadExtend("Thread***3***");
th1.start();
th2.start();
th3.start();
ThreadImp1 ti = new ThreadImp1("runner***1***");
Thread t1= new Thread(ti);
Thread t2 = new Thread("runner***2***");
Thread t3 = new Thread(new ThreadImp1("runner***3***"));
t1.start();
t2.start();
t3.start();
}
}
class ThreadImp1 implements Runnable{ //实现Runable接口
public String name;
ThreadImp1(){
this.name = "";
}
public ThreadImp1(String name){
this.name = name;
}
public String getName(){
return name;
}
@Override
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是"+this.getName()+i);
}
}
}
class ThreadExtend extends Thread{ //继承Thread类
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
}
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是"+this.getName()+i);
}
}
}
1、三种方式删羊
//从后向前数删除n个
int n=2;
int lengh=sheepList.size();
for(int i=lengh-1;i>lengh-n-1;i--)
{
sheepList.remove(i);
}
//删掉指定名字的羊
Iterator it=sheepList.iterator();
while(it.hasNext())
{
sheep sp=(sheep)it.next();
if(sp.getName()=="慢羊羊")
it.remove();
}
//从前面数删除p只羊
int p=2;
for(int i=0;i<p;i++)
{
sheepList.remove(0);
}
2、自动写文件
File file=null;
for(int i=0;i<9;i++)
{
file=new File ("helloworld"+i+".txt");
FileOutputStream os=null;
try {
os=new FileOutputStream(file);
String s="我真"+i;
try {
os.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
3、创建线程的两种方法
一:继承Thread 类,由于Java中是单继承,所以此方法不能继承其他类
二:实现Runnable接口,可以继承父类,可以有多个线程同时操作一个变量
public class MyThread {
public static void main(String[] args) {
ThreadExtend te=new ThreadExtend("Thread***1");
ThreadExtend te1=new ThreadExtend("Thread***2");
ThreadExtend te2=new ThreadExtend("Thread***3");
te.start();
te1.start();
te2.start();
ThreadImpl ti=new ThreadImpl();
Thread t=new Thread(ti);
Thread t2=new Thread(ti);
t.start();
t2.start();
}
}
class ThreadImpl implements Runnable{
private int i;
@Override
public void run() {
for(;i<100;i++){
System.out.println(Thread.currentThread().getName()+"***"+i);
}
}
}
class ThreadExtend extends Thread{
public ThreadExtend(){
}
public ThreadExtend(String name){
super(name);
}
public void run(){
for(int i=0;i<5;i++){
System.out.println("我是"+this.getName()+"*****"+i);
}
}
}
1.删除后三只羊
//第一种方法
int count=sheeplist.size();
for(int i=count-1;i>=count-3;i--){
sheeplist.remove(i);
}
//第二种方法
for(int i=sheeplist.size()-3;i<sheeplist.size();){
sheeplist.remove(i);
}
//第三种方法
int count1=0;
while(count1<3){
sheeplist.remove(sheeplist.size()-1);
count++;
}
//第四种方法
int count2=sheeplist.size();
for(int i=0;i<sheeplist.size();i++){
while(i==count2-3){
sheeplist.remove(i);
i=i-1;
}
}
2.自动写文件
for(int i=0;i<10;i++){
File filei=new File(files, "helloworld"+i+".txt");
try {
OutputStream os=new FileOutputStream(filei);
String s="我真";
s=s+i;
os.write(s.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.线程实现方法继承Thread与接口Runnnale的区别
(1)继承自Thread
ThreadExtend t1=new ThreadExtend("窗口一");
ThreadExtend t2=new ThreadExtend("窗口二");
ThreadExtend t3=new ThreadExtend("窗口三");
t1.start();
t2.start();
t3.start();
class ThreadExtend extends Thread{
private int tickets=5;
public ThreadExtend(String name) {
super(name);
}
@Override
public void run() {
while(this.tickets>0){
this.tickets--;
System.out.println("票在"+this.getName()+"被卖出");
}
}
}
(2)Runnable接口
ThreadExtend t=new ThreadExtend();
Thread c1=new Thread(t,"Runnable1");
Thread c2=new Thread(t,"Runnable2");
Thread c3=new Thread(t,"Runnable3");
c1.start();
c2.start();
c3.start();
class ThreadExtend implements Runnable{
private int ticket=5;
@Override
public void run() {
while(this.ticket>0){
this.ticket--;
System.out.println("票在"+Thread.currentThread().getName()+"被卖出");
}
}
}
继承自Thread只能是单继承,每个线程之间是独立的;Runnnale接口可以实现线程间的资源共享。
方法一:用迭代器来删除后面三只羊
public void iteratorForRemove() {
Iterator<Sheep> it = sheepList.iterator();
int a = sheepList.size() - 1;
int i = 0;
while (it.hasNext()) {
Sheep sheep = (Sheep) it.next();
if (i > a - 3) {
it.remove();
}
i++;
}
}
方法二:用普通for循环来删除后面三只羊
public void forForRemove() {
int a = sheepList.size() - 1;
for (int i = sheepList.size() - 1; i > a - 3; i--) {
sheepList.remove(i);
}
}
方法三:用foreach来删除后面三只羊
public void foreachForRemove() {
for (Sheep sh : sheepList) {
if (i > a - 3) {
sheepList.remove(sh.name);
} else {
System.out.print(sh.name+" ");
}
i++;
}
}
File fe = new File("D://testforio");
try{
if(!fe.exists()){
fe.mkdir();
}
FileOutputStream fos = null;
File file = null;
for(int i = 1;i <= 10;i++){
file = new File(fe,"HelloWorld"+i+".txt");
fos = new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();
}
String str ="我真 "+i;
byte[] b = str.getBytes();
fos.write(b);
fos.flush();
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
class ThreadExtend implements Runnable{
int i = 5;
public void run(){
while(i > 0){
System.out.println(Thread.currentThread().getName()+"得到"+i);
i--;
}
}
}
public class MyRunnable {
public static void main(String[] args) {
ThreadExtend te = new ThreadExtend();
Thread h1 = new Thread(te,"first");
Thread h2 = new Thread(te,"second");
Thread h3 = new Thread(te,"third");
Thread h4 = new Thread(te,"fourth");
h1.start();
h2.start();
h3.start();
h4.start();
}
}
class ThreadExtend extends Thread{
String name;
public ThreadExtend(String name){
this.name = name;
}
int i = 5;
public void run(){
while(i > 0){
System.out.println(name+"得到"+i);
i--;
}
}
}
public class MyThread {
public static void main(String[] args) {
ThreadExtend h1 = new ThreadExtend("first");
ThreadExtend h2 = new ThreadExtend("second");
ThreadExtend h3 = new ThreadExtend("third");
ThreadExtend h4 = new ThreadExtend("fourth");
h1.start();
h2.start();
h3.start();
h4.start();
}
}
import java.util.ArrayList;
public class S111 {
public static void main(String[] args) {
int[] a=new int[]{1,2,3,4,5};
Sheep[] sheep=new Sheep[]{
new Sheep("喜羊羊"),new Sheep("美羊羊"),new Sheep("暖洋洋"),
new Sheep("沸羊羊"),new Sheep("懒洋洋")};
System.out.println(sheep[2].getName());
ArrayList<Sheep> sheepList=new ArrayList();
sheepList.add(new Sheep("喜羊羊"));
sheepList.add(new Sheep("美羊羊"));
sheepList.add(new Sheep("暖洋洋"));
sheepList.add(new Sheep("沸羊羊"));
sheepList.add(new Sheep("懒洋洋"));
int k=sheepList.size();
for(int i=k-3;i<k;i++)
{
sheepList.remove(k-3);
}
for(int i=0;i<3;i++)
{
sheepList.remove(sheepList.size()-1);
}
for (int j=2;j<sheepList.size();j--)
{
sheepList.remove(j);
j++;
}
for(int i=0;i<sheepList.size();i++){
System.out.println(((Sheep)(sheepList.get(i))).getName());
}
}
}
int j=0;
Iterator it = sheepList.iterator();
for(;it.hasNext();){
if(j>1)
{
sheepList.remove(j);
}
else{
it.next();
j++;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.plaf.synth.SynthSeparatorUI;
public class FlieTest {
public static void main(String[] args){
File manyfile=new File("D://test1");
File file;
OutputStream os=null;
try{
if(!manyfile.exists()){
manyfile.mkdir();
}
for(int i=0;i<=10;i++){
file=new File(manyfile,"helloworld"+i+".txt");
os=new FileOutputStream(file);
String s="中北大学安卓实验室";
os.write(s.getBytes());
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(NumberFormatException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
Thread1 s1=new Thread1(); //Thread对象s1只能开启一个线程
Thread1 s2=new Thread1(); //Thread对象s2只能开启一个线程
s1.start();
s2.start();
Thread2 t1=new Thread2();
Thread ta=new Thread(t1,"A");
Thread tb=new Thread(t1,"B"); //使用Runnable接口创建的接口使用同一个对象t1
ta.start();
tb.start();
}
public class Thread1 extends Thread{
public void run(){
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
public class Thread2 implements Runnable{
public void run() {
{
for(int i=0;i<5;i++)
{
System.out.println(Thread.currentThread().getName()
+" "+i);
}
}
}
}
一个Thread对象只能开启一个线程,而一个实现了Runnable接口的对象可以开启多个线程
java是单继承的,使用Runnable
Thread和Runnable的区别
Node:
class ExThread extends Thread{
public ExThread(String name){
super(name);
}
public ExThread(){
super();
}
public void run() {
for(int i=0;i<5;i++){
System.out.println("ExThread"+this.getName()+i);
}
}
}
class ThreadImpl implements Runnable{
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<5;i++){
System.out.println("runable"+this.getName()+i);
}
}
}
创建文件helloword0~9 并输出”我真+i“
Node:
public static void main(String[] args) {
File file;
File Box=new File("F:\\java\\test");
try {
//Box.createNewFile();
if(!Box.exists()){
Box.mkdir();
}
for(int i=0;i<10;i++){
file=new File(Box,"helloword"+i+".txt");
if(!file.exists()){
file.createNewFile();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] filenames=Box.list();//返回当前目录下的所有文件名
for(int i=0;i<filenames.length;i++){
System.out.println(filenames[i]);
File file_del_3=new File(Box+"helloword3.txt");
file_del_3.delete();
}
OutputStream os=null;
for(int i=0;i<filenames.length;i++){
file=new File(Box,filenames[i]);
try {
file.createNewFile();
os= new FileOutputStream(file);
String s="我真"+i;
os.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Node:
for (int i = SheepList.size()-1; i>1; i--) { SheepList.remove(i); }
方法二:从前向后删除
Node:
for(int i=0;i<=SheepList.size();i++){ SheepList.remove(2); }
***作业一:删羊
//方法一:
for (int i=2;i<sheepList .size();i++){
sheepList.remove(i);
}
// sheepList.remove(2);
for(int i = 0;i < sheepList.size();i ++){
System.out.println(((Sheep)(sheepList.get(i))).name);
}
//方法二
for(int i=0;i<sheeps.size();i++){ System.out.println(sheeps.get(i));
} System.out.println("删除后三个");
for(int i=0;i<sheeps.size()-3;i++){
System.out.println(sheeps.get(i));
} System.out.println("删除前两个");
for(int i=sheeps.size()-1;i>sheeps.size()-4;i--){
System.out.println(sheeps.get(i));
}
}
***作业二:添加文件
for (int i=2;i<sheepList .size();i++){
sheepList.remove(i);
}
// sheepList.remove(2);
for(int i = 0;i < sheepList.size();i ++){
System.out.println(((Sheep)(sheepList.get(i))).name);
}
1.删羊:
package cn.edu.nuc.caozhihong;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class TrainForSet {
public static void main(String[] args) {
ArrayList<Sheep> sheepList=new ArrayList();
sheepList.add(new Sheep("美羊羊"));
sheepList.add(new Sheep("懒羊羊"));
sheepList.add(new Sheep("慢羊羊"));
sheepList.add(new Sheep("喜羊羊"));
sheepList.add(new Sheep("沸羊羊"));
sheepList.add(new Sheep("美羊羊"));
System.out.println(sheepList.size());
int lenth=sheepList.size()-1; //1从前向后删
// for(int i=lenth;i>lenth-3;i--){
// sheepList.remove(lenth-2);
// }
// for(int i=lenth;i>lenth-3;i--){ //2从后向前删
// sheepList.remove(i);
// }
for(int i=lenth-3;i<lenth;i++){ //3从后向前删
sheepList.remove(sheepList.size()-1);
}
System.out.println(sheepList.size());
for(Object s:sheepList){
System.out.println(((Sheep)s).name);
}
}
}
2.自动写文件
package cn.nuc.edu.caozhihong;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileTest {
public static void main(String[] args) {
File manyfile=new File("D://test");
File file;
try {
if(!manyfile.exists()){
manyfile.mkdir();
}
for(int i=0;i<10;i++){
file=new File(manyfile,"helloworld"+i+".txt");
file.createNewFile();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] filenames=manyfile.list();
for(int i=0;i<filenames.length;i++){
System.out.println(filenames[i]);
}
File file3=new File(manyfile,"helloworld3.txt");
file3.delete();
for(int i=0;i<9;i++){
file=new File(manyfile,"helloworld"+i+".txt");
OutputStream out=null;
try {
out=new FileOutputStream(file);
String s="我真"+i;
try {
out.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.用代码实现比较Runnable 和 Thread的区别
package cn.edu.nuc.thread;
public class MyThread {
public static void main(String[] args) {
// ThreadExtend h1=new ThreadExtend("one");
// ThreadExtend h2=new ThreadExtend("two");
// ThreadExtend h3=new ThreadExtend("three");
// h1.start();
// h2.start();
// h3.start();
Thread r1=new Thread(new ThreadImp1());
Thread r2=new Thread(new ThreadImp1());
Thread r3=new Thread(new ThreadImp1());
r1.start();
r2.start();
r3.start();
}
}
class ThreadImp1 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是runner"+i);
}
}
}
class ThreadExtend extends Thread{
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是"+this.getName()+i);
}
}
}
作用一样 但是JAVA是但继承 继承了Thread以后不能在继承了。
集合与泛型的应用实例
int length=sheepList.size();
Scanner reader=new Scanner(System.in);
int number=reader.nextInt();
//使用Collection的子类List,List有序的可重复
ArrayList<Sheep> sheepList1=new ArrayList<sheep>();
List<sheep>sheepList=sheepList1;
//遍历一遍羊的个数,因为是有序的所以有两种方法
//方法一:下标索引遍历
for(int i=0;i<length;i++){
System.out.println(i);
}
//方法二:使用迭代器遍历删除
Iterator<sheep>it=set.iterator();
if(it.hasNext()){
String s=it.next();
it.remove();
System.out.println("删除的元素为"+s);
}
//删羊
//方法一:正常删除顺序,删除number个
for(int i=0;i<number;i++){
sheepList.remove(i);//因为List是有序的,所以可以使用下标索引
}
//方法二:从后向前删number只羊
for(int i=length;i>=length-number;i--){
sheepList.remove(i);
}
IO的基本使用与操作
//IO的普通操作
FileInputStream fis=null;
FileOutputStream fos=null;
//IO异常是checkException,需要显性做出处理异常
try{
//生成输入流的对象
fis=new FileInputStream("e:hello.txt");
//生成输出流的对象
fos=new FileOutputStream("e:hello1.txt");
//生成一个字节数组
byte[] buffer=new byte[1024];
while(true){
int temp=fis.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
fos.write(buffer,0,temp);
}
catch(IOException e){
System.out.println(e);
}
finally{
try{//仍然是checkException,所以关闭时候也要捕获异常
fis.close();
fos.close();
}
catch{
System.out.println(e);
}
}
//自动写文件
for(int i=0;i<9;i++){ //设置自动生成10个文件(0-9)
file=new File(manyfile,"helloworld"+i+".txt");//manyfile为文件路径
OutputStream out=null;
try {
out = new FileOutputStream(file);
String s = "我是"+i; //写入文件的内容
try {
out.write(s.getBytes());
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try{//仍然是checkException,所以关闭时候也要捕获异常
out.close();
}
catch{
System.out.println(e);
}
}
}
线程的两种创建方法
//创建线程的方法一
//定义一个线程类,它继承Thread并重新其中的run()方法。
class FirstThread extends Thread {
public void run(){
for(int i=0;i <100;i++){
System.out.println("FirstThread-->"+i);
}
}
}
public static void main(String args[]){
//生成线程对象
FirstThread firstThread=new FirstThread();
//启动线程
firstThread.start();
//主方法也是一个线程
for(int i=0;i <100;i++){
System.out.println("main-->"+i);
}
}
//创建线程的方法二
/*提供一个实现接口Runnable的类作为线程的目标对象,在初始化一个Thread类或者Thread子类的线程对象时,把目标
对象传递给这个线程的实例,由该目标对象提供线程体*/
class SecondThread implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("SecondThread-->"+i);
}
}
}
public static void main(String args[]){
//生成一个Runnable接口实现类的对象
SecondThread two =new SecondThread();
//生成一个Thread对象,并将twoz作为参数传递给Thread对象
Thread t=new Thread(two);
//执行线程
t.start();
}
1.批量创建文件
package cn.edu.nuc;
import java.io.File;
import java.io.FileNotFoun-dException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileTest1{
public static void main(String[] args){
File directory = new File("D:"+File.separator+"direc");
OutputStream output = null;
if (!directory.exists()) //若不存在“direc”这个文件夹,则创建
directory.mkdir();
try{ //创建10个txt文件
for (int i = 0; i < 10; i++){
File file = new File("D:"+File.separator+"direc"+File.separator+"hello"+ i +".txt");
if (!file.exists())
file.createNewFile();
}
}catch(IOException e){
e.printStackTrace();
}
String[] fileNames = directory.list(); //把directory中的文件名字保存在数组中
for (int i = 0; i < 10; i ++){ //向文件中写入
File hellos = new File("D:"+File.separator+"direc"+File.separator+"hello"+ i +".txt");
try{
FileOutputStream out = new FileOutputStream(hellos);
String str = new String("hello world "+ i);
byte[] bstr = str.getBytes();
out.write(bstr);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
2.删除羊
package cn.edu.nuc;
import java.util.ArrayList;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args){
ArrayList sheepList = new ArrayList();
sheepList.add(new sheep("喜羊羊"));
sheepList.add(new sheep("美羊羊"));
sheepList.add(new sheep("懒羊羊"));
sheepList.add(new sheep("沸羊羊"));
sheepList.add(new sheep("慢羊羊"));
sheepList.add(new sheep("暖羊羊"));
for (sheep s : sheepList)
System.out.println(s.name);
int n = 3; //删除羊的只数
int length = sheepList.size();
//从后面删除n只羊
for (int i = length -1; i >= length - n; i--) {
sheepList.remove(i);
}
//从前面删除n只羊
for (int i = 0; i < n; i++)
sheepList.remove(i);
Iterator it = sheepList.iterator();
//删除指定的羊
sheep goalSheep = new sheep("慢羊羊");
while (it.hasNext()){
sheep s = (sheep) it.next();
if (s.equals(goalSheep))
it.remove();
}
for (sheep s : sheepList)
System.out.println(s.name);
}
}
class animal{
public String name = "动物";
}
class sheep extends animal{
public String name;
private String head;
private String froot;
public sheep(String name){
this.name = name;
super.name = name;
}
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0:name.hashCode());
return result;
}
public boolean equals(Object obj){
if (this.name.equals(((sheep)obj).name)){
return true;
}else
return false;
}
}
3.多线程
package cn.edu.nuc;
public class Multithreading {
public static void main(String[] args){
SubThread s1 = new SubThread("a---");
SubThread s2 = new SubThread("b---");
SubThread1 s11= new SubThread1("c---");
SubThread1 s12 = new SubThread1("d---");
s1.start();
s2.start();
s1.run();
s2.run();
s11.run();
s12.run();
}
}
//subthread 单继承thread
class SubThread extends Thread{
private String name;
public SubThread(){
}
public SubThread(String name){
super(name);
}
/*public String name(){
return this.name;
}*/
public void run(){
for (int i = 0; i < 5; i ++){
System.out .println("这是同时进行的第 " + i + " 个"+this.getName());
}
}
}
class SubThread1 implements Runnable{
private String name;
public SubThread1(String name){
this.name = name;
}
public SubThread1(){
}
public void run(){
for (int i = 0; i < 5; i ++)
System.out.println("这是同时进行的第 "+ i + "个"+this.getName());
}
private String getName() {
// TODO Auto-generated method stub
return this.name;
}
}
int len=sheepList.size();
int count=0;
Iterator<Sheep> itr=sheepList.iterator();
while(itr.hasNext()){
if(count>len-4){
itr.next();
itr.remove();
}
else {itr.next();
count++;
}
}
package cn.edu.nuc.NewClass4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest");
File file1 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2");
File file2 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\helloworld.txt");
File file3 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld1.txt");
File file4 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld2.txt");
File file5 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld3.txt");
File file6 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld4.txt");
try {
file.mkdir();
file1.mkdirs();
file2.createNewFile();
file3.createNewFile();
file4.createNewFile();
file5.createNewFile();
file6.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] name=file.list();
for(int i=0;i<name.length;i++){
System.out.println(name[i]);
}
OutputStream IO = null;
File[] Name=null;
Name=file1.listFiles();
System.out.println(Name.length);
for(int i=0;i<Name.length;i++){
try {
IO=new FileOutputStream(Name[i], true);
String s="中北大学安卓实验室";
IO.write(s.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}try {
IO.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package cn.edu.nuc.NewClass5;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadExtend H1=new ThreadExtend("一号");
ThreadExtend H2=new ThreadExtend("二号");
ThreadExtend H3=new ThreadExtend("三号");
H1.start();
H2.start();
H3.start();
ThreadImpl h=new ThreadImpl("一号");
new Thread(h).start();
new Thread(h).start();
new Thread(h).start();
}
}
class ThreadImpl implements Runnable{
private int number =10;
private String name;
public ThreadImpl(String name) {
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("我是实现Runnable");
for(int i=0;i<10;i++){
System.out.println("Runnable线程:"+this.name+",i="+number--);
if(number<1)
break;
}
}
}
class ThreadExtend extends Thread{
private int number =10;
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run(){
for(int i=0;i<10;i++){
if(number<1)
break;
System.out.println("Thread线程:"+this.getName()+number--);
}
}
}
<TextView
android:layout_weight="1"
android:text="中"
android:gravity="center"
android:textSize="50sp"
android:layout_width="0dp"
android:background="#f0f"
android:layout_height="match_parent"
/>
<TextView
android:layout_weight="1"
android:text="北"
android:gravity="center"
android:textSize="50sp"
android:layout_width="0dp"
android:background="#ff0"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:text="大"
android:gravity="center"
android:textSize="50sp"
android:layout_width="0dp"
android:background="#0ff"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:text="学"
android:gravity="center"
android:textSize="50sp"
android:layout_width="0dp"
android:background="#f0f"
android:layout_height="match_parent"/>
int lenth=sheepList.size()-1; //1从前向后删
// for(int i=lenth;i>lenth-3;i--){
// sheepList.remove(lenth-2);
// }
// for(int i=lenth;i>lenth-3;i--){ //2从后向前删
// sheepList.remove(i);
// }
for(int i=lenth-3;i<lenth;i++){ //3从后向前删
sheepList.remove(sheepList.size()-1);
}
Iterator<Sheep> itr=sheepList.iterator(); //迭代器
int n=0;
while(itr.hasNext()){
if(n>lenth-3){
Object o=itr.next();
System.out.println(((Sheep)o).name);
itr.remove();
}
else{
itr.next();
n++;
}
}
for(int i=0;i<9;i++){
file=new File(manyfile,"helloworld"+i+".txt");
OutputStream out=null;
try {
out=new FileOutputStream(file);
String s="我真"+i;
try {
out.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadExtend h1=new ThreadExtend("one");
ThreadExtend h2=new ThreadExtend("two");
ThreadExtend h3=new ThreadExtend("three");
h1.start();
h2.start();
h3.start();
ThreadImp1 thr=new ThreadImp1();
Thread r1=new Thread(thr);
Thread r2=new Thread(thr);
Thread r3=new Thread(thr);
r1.start();
r2.start();
r3.start();
}
}
class ThreadImp1 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是runner"+i);
}
}
}
class ThreadExtend extends Thread{
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是"+this.getName()+i);
}
}
//Delete_Sheep================================================
ArrayList<sheep> sheeplist=new ArrayList<sheep>();
sheeplist.add(new sheep("1"));
sheeplist.add(new sheep("2"));
sheeplist.add(new sheep("3"));
sheeplist.add(new sheep("4"));
sheeplist.add(new sheep("5"));
sheeplist.add(new sheep("6"));
int i=0;
// /*方法一*/:for(i=5;i>2;i--)
// sheeplist.remove(sheeplist.get(i));
//
//
// /*方法二*/:for(i=0;i<3;i++)
// sheeplist.remove(sheeplist.get(3));
//
// /*方法三*/:while(sheeplist.size()!=3)
// sheeplist.remove(sheeplist.get(3));
for(i=0;i<3;i++)
System.out.println(((sheep)(sheeplist.get(i))).getName());
//======================================================================
//Create_FileAuto=========================================================
public class fileTEST {
public static void main(String[] args) {
//建立hello world文件
File manyfile=new File("D:\\test");
manyfile.mkdir();
File file = null;
try {
manyfile.mkdir();
for(int i=0;i<10;i++)
{
file=new File(manyfile,"helloworld"+i+".txt");
file.createNewFile();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
for(int i=0;i<10;i++)
{
OutputStream os=new FileOutputStream("D:\\test\\helloworld"+i+".txt");
String s="andriod lab"+i;
os.write(s.getBytes());
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
InputStream is=new FileInputStream("D:\\test\\helloworld"+0+".txt");
byte[] read=new byte[1024];
is.read(read);
System.out.println();
} catch (Exception e) {
// TODO: handle exception
}
}
}
//============================================================
//Threads=====================================================
public class cyh {
public static void main(String[] args) {
threadx thread1=new threadx();
threadx thread2=new threadx();
thread1.start();
thread2.start();
thready thread3=new thready();
thready thread4=new thready();
Thread thread5=new Thread(thread3);
Thread thread6=new Thread(thread4);
thread5.start();
thread6.start();
}
}
class threadx extends Thread{
public void run(){
System.out.println("openThread");
}
static int a=0;
}
class s{
}
class thready extends s implements Runnable{
public void run() {
System.out.println("openRunnable");
}
}
int n=2;
int lengh=sheepList.size();
for(int i=lengh-1;i>lengh-n-1;i--)
{
sheepList.remove(i);
}
Iterator it=sheepList.iterator();
while(it.hasNext())
{
sheep sp=(sheep)it.next();
if(sp.getName()=="慢羊羊")
sheepList.remove(sp);
}
int p=2;
for(int i=0;i<p;i++)
{
sheepList.remove(0);
}
for(int i=0;i<9;i++){
file=new File(manyfile,"helloworld"+i+".txt");
OutputStream out=null;
try {
out=new FileOutputStream(file);
String s="我真"+i;
try {
out.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] arge){
ThreadExtend th=new ThreadExtend("1--");
ThreadExtend th1=new ThreadExtend("2--");
ThreadExtend th2=new ThreadExtend("3--");
th.start();
th.start();
th1.start();
th2.start();
Thread th3=new Thread(new ThreadImpl("1--"));
Thread th4=new Thread(new ThreadImpl("2--"));
Thread th5=new Thread(new ThreadImpl("3--"));
th3.start();
th3.start();
th4.start();
th5.start();
}
}
class ThreadImpl implements Runnable{
private String name;
public ThreadImpl(String name){
this.name=name;
}
public ThreadImpl(){
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++){
System.out.println("i am"+this.getName()+i);
}
}
private String getName() {
// TODO Auto-generated method stub
return name;
}
}
class ThreadExtend extends Thread{
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++){
System.out.println("i am"+this.getName()+i);
}
}
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="4"
android:layout_height="0dp"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#f0f"
android:layout_height="match_parent"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#ff0"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#0ff"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#00f"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical">
<TextView
android:background="#0f0"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<TextView
android:layout_weight="1"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<TextView
android:layout_weight="1"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
</LinearLayout>
int lenth=sheepList.size()-1; //1从前向后删
//for(int i=lenth;i>lenth-3;i--){
// sheepList.remove(lenth-2);
//}
//for(int i=lenth;i>lenth-3;i--){ //2从后向前删
// sheepList.remove(i);
//}
for(int i=lenth-3;i<lenth;i++){ //3从后向前删
sheepList.remove(sheepList.size()-1);
}
for(int i=0;i<9;i++){
file=new File(manyfile,"helloworld"+i+".txt");
OutputStream out=null;
try {
out=new FileOutputStream(file);
String s="我真"+i;
try {
out.write(s.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thread
和runnable
的比较public static void main(String[] args) {
// ThreadExtend h1=new ThreadExtend("one");
// ThreadExtend h2=new ThreadExtend("two");
// ThreadExtend h3=new ThreadExtend("three");
// h1.start();
// h2.start();
// h3.start();
Thread r1=new Thread(new ThreadImp1());
Thread r2=new Thread(new ThreadImp1());
Thread r3=new Thread(new ThreadImp1());
r1.start();
r2.start();
r3.start();
}
class ThreadImp1 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是runner"+i);
}
}
}
class ThreadExtend extends Thread{
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run(){
for(int i=0;i<100;i++){
System.out.println("我是"+this.getName()+i); //???
}
}
}
两种方式效果一样,但是JAVA是单继承,继承了Thread类以后就不能再继承了。
删羊操作
//利用迭代器遍历羊群并删除。
int i;
public void Iterator{
Sheep[] sheep = new Sheep[] {new Sheep("喜羊羊"), new Sheep("美羊羊"), new Sheep("慢羊羊"), new Sheep("沸羊羊"),
new Sheep("懒羊羊")};
Iterator sum=sheep.Iterator();
while(sum.hasNext()){
Sheep sheep=(Sheep)sum.next();
sum.remove();
}
}
//寻常的for循环从前向后删
for(i=1;i<sheepList.size()+1;i++){
sheepList.remove(i);
}
//从后向前删
for(i=sheepList.size()-1;i>0;i--){
sheepList.remove(i);
}
自动写文件
// 先生成文件,然后再执行出错跟踪和输入操作。
int a;
Scanner reader=new Scanner(System.in);
a=reader.nextInt();
while(a){
File file=new File("D://helloworld"+i+".txt");
OutputStream outputstream=null;
try{
outputstream=new FileOutputStream(file);
String s="我是"+i;
outputstream.write(s)
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
file.close();
}
catch(IOException e){
e.printStackTrace()
}
}
}
实现Thread和Runnable接口的比较
//Thread和Runnable的区别:
// Thread是类,在Thread中只能利用start方法实现run方法,且在执行时需要等待。
// Runnable是接口,被继承时需要重写run方法,在现实该方法时也要利用start方法,且在
// 执行时不等待 ,直接抢先执行。
public HtThread extends Thread{
private int i;
public void run(){
for(i=0;i<=100;i++){
System.out.println("HtThread-->"+i);
}
}
public HtRunnable implementsRunnable{
private int i;
public void run(){
for(i=0;i<=100;i++){
System.out.println("HtRunnable-->"+i);
}
}
public static void main(String args[]){
HtThread thread=new HtThread();
HtRunnable runnable=new HtRunnable();
thread.start();
for(int i=0;i<=100;i++){
System.out.println("test-->"+i);
}
//runnable.start();
}
作业:线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:layout_weight="4" android:layout_height="0dp" android:layout_width="match_parent" > <TextView android:layout_weight="1" android:text="你好安卓" android:layout_height="match_parent" android:layout_width="0dp" android:background="#0ff"/> <TextView android:layout_weight="1" android:text="你好安卓" android:layout_height="match_parent" android:layout_width="0dp" android:background="#fff"/> <TextView android:layout_weight="1" android:text="你好安卓" android:layout_height="match_parent" android:layout_width="0dp" android:background="#eff"/> <TextView android:layout_weight="1" android:text="你好安卓" android:layout_height="match_parent" android:layout_width="0dp" android:background="#aff"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical" >
<TextView
android:layout_weight="1"
android:layout_height="0dp"
android:text="你好安卓"
android:layout_width="match_parent"
android:background="#ca0"/>
<TextView
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:text="你好安卓"
android:background="#c20"/>
<TextView
android:layout_weight="1"
android:layout_height="0dp"
android:text="你好安卓"
android:layout_width="match_parent"
android:background="#a00"/>
</LinearLayout>
<TextView
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#fff" />
<TextView
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#f0f"
/>
</linearLayout>
<linearLayout
android:layout_weight="3"
android:layout_height="0dp"
android:layout_width="match_parent"
android:orientation="vertical"
>
<TextView
android:text="Hello"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#ff0" />
<TextView
android:text="Android"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#f0f" />
<TextView
android:text="实验室"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#ff0"
/>
1.删羊
//删除羊的三种办法
//1 顺序递增删羊
int k=sheepList.size();
for(int i=k-3;i<k;i++)
{
sheepList.remove(k-3);
}
//2 顺序递减删羊
int k=sheepList.size();
for(int i=k-1;i>=k-3;i--)
{
sheepList.remove(i);
}
//3 从后向前删
for(int i=0;i<3;i++)
{
sheepList.remove(sheepList.size()-1);
}
2.IO文件
package javahomework;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class doing2 {
public static void main(String[] args) {
File manyfile=new File("D://testfoio");
File file = null ;
try {
if(!manyfile.exists())
manyfile.mkdir();
for(int i=0;i<10;i++)
{file=new File(manyfile.getAbsolutePath(),"helloworld"+i+".txt");
if(!file.exists())
file.createNewFile();}
} catch (IOException e) {
e.printStackTrace();
}
String []filenames= manyfile.list();
for(int i=0;i<filenames.length;i++)
System.out.println(filenames[i]);
File file_3=new File(manyfile,"helloworld_3.txt");
file_3.delete();
for(int i=0;i<=9;i++)
{
file =new File(manyfile,"helloworld"+i+".txt");
OutputStream os=null;
try {
os=new FileOutputStream(file);
String s="我真"+i;
try {
os.write(s.getBytes());
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
} catch (FileException e) {
e.printStackTrace();
}finally
{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}}
}
}
}
比较Thread和Runnable
class othread extends Thread{
public othread(String name){
super(name);
}
public othread(){
super();
}
public void run() {
for(int i=0;i<5;i++){
System.out.println("othread"+this.getName()+i);
}
}
}
class threadexp implements Runnable{
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<5;i++){
System.out.println("runable"+this.getName()+i);
}
}
}
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#f0f"
android:layout_height="match_parent"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#ff0"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#0ff"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#00f"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_weight="4"
android:layout_height="0dp"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#f0f"
android:layout_height="match_parent"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#ff0"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#0ff"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:background="#4ef10d"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical">
<TextView
android:background="#46deec"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<TextView
android:layout_weight="1"
android:background="#b14c54"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<TextView
android:layout_weight="1"
android:background="#475297"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
</LinearLayout>
作业一:删羊
package qrj.com;
import java.util.ArrayList;
public class Java_homework1 {
public static void main(String[] args) {
ArrayList sheepList=new ArrayList();
sheepList.add(new Sheep("喜洋洋"));
sheepList.add(new Sheep("懒洋洋"));
sheepList.add(new Sheep("美洋洋"));
sheepList.add(new Sheep("沸洋洋"));
sheepList.add(new Sheep("慢洋洋"));
//删羊方法一:
int len=sheepList.size(); //从后往前删
for(int i=sheepList.size()-1;i>1;i--){
sheepList.remove(i);
}
//方法二:
for(int i=0;i<3;i++){ //从末尾删三次
sheepList.remove(sheepList.size()-1);
}
//方法三:
for(int i=2;i<sheepList.size();){
sheepList.remove(i);
}
//方法四:
for(int i=0;i<sheepList.size();i++){ //删除羊(方法特殊)
sheepList.remove(sheepList.size()-1);
}
//输出方法一:
for(int i=0;i<sheepList.size();i++){
System.out.println(((Sheep)(sheepList.get(i))).name); //这里注意格式转化
}
//输出方法二:
for(Object s:sheepList){ //for(元素类型 元素名,List)
System.out.println(((Sheep)s).name);
}
}
}
作业二:文件操作
package qrj.com;
import java.io.*;
public class Java_homework2 {
public static void main(String[] args) {
File file=new File("G://java");
File myfile=null;
OutputStream os=null;
try {
if(!file.exists()){
file.mkdir();
}
for(int i=0;i<10;i++){
myfile = New File(file.getAbsoluteFile(),"java "+i+" .txt");
if(!myfile.exists())
myfile.createNewFile();
try {
os=new FileOutputStream(myfile);
String s="我真"+i;
try {
os.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
作业三:两种创建并启动线程方法的比较
package qrj.com;
public class Java_homework3 {
public static void main(String[] args) {
//创建并启动继承线程
myThread t1=new myThread("the fist");
myThread t2=new myThread("the second");
myThread t3=new myThread("the thrid");
t1.start();
t2.start();
t3.start();
//创建并启动接口线程
Thread s1=new Thread(new Thread2("the 1.."));
Thread s2=new Thread(new Thread2("the 2.."));
Thread s3=new Thread(new Thread2("the 3.."));
s1.start();
s2.start();
s3.start();
}
}
class myThread extends Thread{ //Thread的继承类
public myThread(String name){
super(name);
}
public myThread(){
super();
}
public void run(){
for(int i=0;i<3;i++){
System.out.println("继承类..."+this.getName()+i);
}
}
}
class Thread2 implements Runnable{ //实现接口的类
public String name;
public Thread2(){}
public Thread2(String name){
this.name=name;
}
public String getName(){
return name;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println("实现接口的类..."+this.getName()+i);
}
}
public void setName(String name){
this.name=name;
}
}
实现Runnable接口和继承Thread类创建并启动线程的区别:
前者可共享同一资源,后者不可;前者用途比较广,不可用this直接显示当前线程;后者操作简单,可以用this显示当前线程。最总要的一点,前者可以共用同一资源,后者不可。
Iterator<Sheep> iter=sheepList.iterator();
int num=0;
while(iter.hasNext()&&num<3){
Sheep l=(Sheep)iter.next();
// 1. if((l.name).equals("懒羊羊")){
// iter.remove();
// }
// 2. if(sheepList.size()>2){
// iter.remove();
// }
// 3. iter.remove();
// num++;
}
for(int i=0;i<sheepList.size();i++)
{
//Sheep l=iter.next();
System.out.println(((Sheep)(sheepList.get(i))).name);
}
String[] filenames=manyfile.list();
for(int i=0;i<filenames.length;i++){
String path=filenames[i];
file=new File(manyfile,filenames[i]);
FileOutputStream oos=null;
String s="我真"+i;
try {
oos=new FileOutputStream(file);
try {
oos.write(s.getBytes());
//oos.write(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadExtend h1=new ThreadExtend("一号");
ThreadExtend h2=new ThreadExtend("二号");
ThreadExtend h3=new ThreadExtend("三号");
h1.start();
h2.start();
h3.start();//Thread
Thread r1=new Thread(new ThredImp1());
Thread r2=new Thread(new ThredImp1());
Thread r3=new Thread(new ThredImp1());
r1.start();//线程不能重复启动
r2.start();
r3.start();//runnable
}
}
class ThredImp1 implements Runnable{
public String name;
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
class ThreadExtend extends Thread{
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run(){
for(int i=0;i<5;i++){
System.out.println("我是"+this.getName()+i);
}
}
//runnable和thread的区别:
//.1.;避免单继承的局限性2.便于资源共享
作业一:删羊
package qrj.com;enter code here
import java.util.ArrayList;
public class Java_homework1 {
public static void main(String[] args) {
ArrayList sheepList=new ArrayList();
sheepList.add(new Sheep("喜洋洋"));
sheepList.add(new Sheep("懒洋洋"));
sheepList.add(new Sheep("美洋洋"));
sheepList.add(new Sheep("沸洋洋"));
sheepList.add(new Sheep("慢洋洋"));
//删羊方法一:
int len=sheepList.size(); //从后往前删
for(int i=sheepList.size()-1;i>1;i--){
sheepList.remove(i);
}
//方法二:
for(int i=0;i<3;i++){ //从末尾删三次
sheepList.remove(sheepList.size()-1);
}enter code here
//方法三:
for(int i=2;i<sheepList.size();){
sheepList.remove(i);
}
//方法四:
for(int i=0;i<sheepList.size();i++){ //删除羊(方法特殊)
sheepList.remove(sheepList.size()-1);
}
//输出方法一:
for(int i=0;i<sheepList.size();i++){
System.out.println(((Sheep)(sheepList.get(i))).name); //这里注意格式转化
}
//输出方法二:
for(Object s:sheepList){ //for(元素类型 元素名,List)
System.out.println(((Sheep)s).name);
}
}
}
作业二:文件操作
package qrj.com;
import java.io.*;
public class Java_homework2 {
public static void main(String[] args) {
File file=new File("G://java");
File myfile=null;
OutputStream os=null;
try {
if(!file.exists()){
file.mkdir();
}
for(int i=0;i<10;i++){
myfile = New File(file.getAbsoluteFile(),"java "+i+" .txt");
if(!myfile.exists())
myfile.createNewFile();
try {
os=new FileOutputStream(myfile);
String s="我真"+i;
try {
os.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
}enter code here
}
作业三:两种创建并启动线程方法的比较
package qrj.com;
public class Java_homework3 {
public static void main(String[] args) {
//创建并启动继承线程
myThread t1=new myThread("the fist");
myThread t2=new myThread("the second");
myThread t3=new myThread("the thrid");
t1.start();
t2.start();
t3.start();
//创建并启动接口线程
Thread s1=new Thread(new Thread2("the 1.."));
Thread s2=new Thread(new Thread2("the 2.."));
Thread s3=new Thread(new Thread2("the 3.."));
s1.start();
s2.start();
s3.start();
}
}
class myThread extends Thread{ //Thread的继承类
public myThread(String name){
super(name);
}
public myThread(){
super();
}
public void run(){
for(int i=0;i<3;i++){
System.out.println("继承类..."+this.getName()+i);
}
}
}
class Thread2 implements Runnable{ //实现接口的类
public String name;
public Thread2(){}
public Thread2(String name){
this.name=name;
}
public String getName(){
return name;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println("实现接口的类..."+this.getName()+i);
}
}
public void setName(String name){
this.name=name;
}
}
实现Runnable接口和继承Thread类创建并启动线程的区别:
前者可共享同一资源,后者不可;前者用途比较广,不可用this直接显示当前线程;后者操作简单,
可以用this显示当前线程。最总要的一点,前者可以共用同一资源,后者不可。
222
>1.删羊
package javahomework;
import java.util.ArrayList;
public class homework1 {
public static void main(String[] args) {
ArrayList sheeplist = new ArrayList();
sheeplist.add(new sheep("喜羊羊"));
sheeplist.add(new sheep("美羊羊"));
sheeplist.add(new sheep("懒羊羊"));
sheeplist.add(new sheep("慢羊羊"));
sheeplist.add(new sheep("沸羊羊"));
sheeplist.add(new sheep("美羊羊"));
1.
for (int i = sheeplist.size()-1;i>=3;i--)
sheeplist.remove(i);
for(Object s:sheeplist)
{System.out.println(((sheep)s).name);}
2.
for (int i = 0; i < sheeplist.size(); i++)
{
if (i==3)
{ for(int j=i;j<sheeplist.size();j++)
{sheeplist.remove(j);
j--;}
}
else System.out.println(((sheep) (sheeplist.get(i))).name);
}
3.
for(int i=0;i<sheeplist.size();i++)
{
if(i==3)
{sheeplist.remove(i);
i--;}
}
for(Object s:sheeplist)
{System.out.println(((sheep)s).name);}
4.
int len=sheeplist.size();
int count=0;
Iterator<sheep> itr=sheeplist.iterator();
while(itr.hasNext())
{
if(count>len-4)
{Object ob=itr.next();
itr.remove();
}
else {itr.next();
count++;}
}
for(Object s:sheeplist)
{System.out.println(((sheep)s).name);}
};
}
}
>1.删羊
package javahomework;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class homework2 {
public static void main(String[] args) {
File manyfile=new File("D://testfoio");
File file = null ;
try {
if(!manyfile.exists())
manyfile.mkdir();
for(int i=0;i<10;i++)
{file=new File(manyfile.getAbsolutePath(),"helloworld"+i+".txt");
if(!file.exists())
file.createNewFile();}
} catch (IOException e) {
e.printStackTrace();
}
String []filenames= manyfile.list();
for(int i=0;i<filenames.length;i++)
System.out.println(filenames[i]);
File file3=new File(manyfile,"helloworld3.txt");
file3.delete();
for(int i=0;i<=9;i++)
{
file =new File(manyfile,"helloworld"+i+".txt");
OutputStream os=null;
try {
os=new FileOutputStream(file);
String s="我真"+i;
try {
os.write(s.getBytes());
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally
{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}}
}
}
}
继承Thread类的,我们相当于拿出三件事即三个卖票10张的任务分别分给三个窗口,他们各做各的事各卖各的票各完成各的任务,因为MyThread继承Thread类,所以在new MyThread的时候在创建三个对象的同时创建了三个线程;
实现Runnable的, 相当于是拿出一个卖票10张得任务给三个人去共同完成,new MyThread相当于创建一个任务,然后实例化三个Thread,创建三个线程即安排三个窗口去执行。
>3.thread与runnable区别
package javahomework;
class thread extends Thread
{ public int ticket=10;
public String name;
public thread(String name)
{this.name=name;}
public void run()
{
for(int i=0;i<500;i++)
if(ticket>0)
{System.out.println(this.name+"卖"+(ticket--));}
}
}
public class thread1 {
public static void main(String[] args) {
thread t1=new thread("1号窗");
thread t2=new thread("2号窗");
thread t3=new thread("3号窗");
t1.start();
t2.start();
t3.start();
}
}
package javahomework;
class runnable1 implements Runnable
{ public int ticket=10;
public String name;
public void run()
{
for(int i=0;i<500;i++)
if(ticket>0)
{System.out.println(thread.currentThread().getName()+"卖"+(ticket--));}
}
}
public class runnabke {
public static void main(String[] args) {
runnable1 mt=new runnable1();
Thread t1 = new Thread(mt,"1号窗");
Thread t2 = new Thread(mt,"2号窗");
Thread t3 = new Thread(mt,"3号窗");
t1.start();
t2.start();
t3.start();
}
}
1 删除羊
Iterator<String> sheepss = sheeps.iterator();
int i = 0;
方法一
while (sheepss.hasNext()) {
System.out.println(sheepss.next());
i++;
if (i > 2) break;
}
for(Iterator sheepss1 = sheeps.iterator();sheepss1.hasNext();){
System.out.println(sheepss1.next());
}
方法二
for (Object str : sheeps) {
if (i < 3) {
System.out.println(str.toString());
}
i++;
//i>3的时候删去前面的
}
方法三
for(int i=0;i<sheeps.size();i++){ System.out.println(sheeps.get(i));
} System.out.println("删除后三个");
for(int i=0;i<sheeps.size()-3;i++){
System.out.println(sheeps.get(i));
} System.out.println("删除前两个");
for(int i=sheeps.size()-1;i>sheeps.size()-4;i--){
System.out.println(sheeps.get(i));
}
}
2 Thread Runnable
//Runnable 代码被多个线程共享;
public void run() {
while (num > 0) {
num--;
System.out.println(Thread.currentThread().getName() + "减少了一" + " "
+ num);
}
}
MyThread a = new MyThread();
Thread b = new Thread(a, "one");
Thread c = new Thread(a, "two");
Thread d = new Thread(a, "three");
b.start();
c.start();
d.start();
//Thread JAVA单继承方式
public void run() {
while (num > 0) {
num--;
System.out.println(name + "减少了一" + " " + num);
}
}
Mythread a = new Mythread("first");
Mythread b = new Mythread("second");
Mythread c = new Mythread("third");
a.start();
b.start();
c.start();
3写入文件我真 i
for (int i = 0; i < 10; i++) {
file = new File(manyfile, "helloworld" + i + ".txt");
if (!file.exists()) {
file.createNewFile();
}
}
for (int i = 0; i <= 9; i++) {
file = new File(manyfile, "helloworld" + i + ".txt");
OutputStream os = null;
try {
os = new FileOutputStream(file);
String s = "我真" + i;
try {
os.write(s.getBytes());
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
作业标题
代码
线程**********************************************
class A implements Runnable
{ private String name ;
private int Apple;
public A(String name){
this.name = name;
}
public void run(){
for (Apple = 6;Apple >= 0 ;Apple-- )
{
System.out.println(name + "卖苹果:"+Apple);
}
}
}
public class ceshi extends Thread
{
private String name;
private int ticket;
public ceshi(String name){
this.name = name;
}
public void run(){
for (ticket = 5 ;ticket>= 0 ;ticket-- )
{
System.out.println(name+"卖票: "+ticket);
}
}
public static void main(String[] args){
ceshi c1 = new ceshi("A"); //资源不共享
ceshi c2 = new ceshi("B");
ceshi c3 = new ceshi("C");
c1.start();
c2.start();
c3.start();
System.out.println("*************************");
A C = new A("老王"); //资源达到共享
new Thread(C).start();
new Thread(C).start();
new Thread(C).start();
}
}
*********************************************************、
删羊: List<String> AllList = null;
AllList = new ArrayList<String>();
AllList.add("Hello1");
AllList.add("Hello2");
AllList.add("Hello3");
AllList.add("Hello4");
AllList.add("Hello5");
System.out.println(AllList);
//System.out.println(AllList.get(0));
List<String>SubList = AllList.subList(2,4);//通过截取部分集合删除羊1
for (int i = 0;i <SubList.size();i++){
System.out.println(SubList.get(i)+" ");
}
for (int i = 0;i <AllList.size()-2;i++){ //删除羊2
System.out.print(AllList.get(i+2)+" ");
}
System.out.println();
for (int i = 0 ; i < 3; i++){ //删除羊3
AllList.remove(i);
}
System.out.println(AllList);
***********************************************************************
写文件:
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class IOdemo
{
public static void main(String[] args)throws Exception{
File f = new File("e:"+File.separator+"text.txt");
OutputStream out = new FileOutputStream(f);
String st = "Hello world";
byte b[] = st.getBytes();
for (int i = 0;i<b.length ;i++ )
{
out.write(b[i]);
}
out.close();
}
}
int a = sheepList.size() - 1;
for(int i=sheepList.size() - 1 ; i >= a - 3 ; i --){
sheepList.remove(i);
}
System.out.println(sheepList);
File fe = new File("b://leihous");
try{
if(!fe.exists()){
fe.mkdir();
}
FileOutputStream nu = null;
File file = null;
for(int i = 1;i <= 10;i++){
file = new File(fe,"HelloWorld"+i+".txt");
nu = new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();
}
String str ="我真 "+i;
byte[] b = str.getBytes();
nu.write(b);
nu.flush();
nu.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
//thread是单继承,runnable可以多个线程同时处理同一个变量
private int ticket = 10;
private String name;
public MyThread(String name){
this.name =name;
}
public void run(){
for(int i =0;i<500;i++){
if(this.ticket>0){
System.out.println(this.name+"卖票---->"+(this.ticket--));
}
}
}
}
public static void main(String[] args) {
MyThread mt1= new MyThread("一号窗口");
MyThread mt2= new MyThread("二号窗口");
MyThread mt3= new MyThread("三号窗口");
mt1.start();
mt2.start();
mt3.start();
}
}
private int ticket = 10;
private String name;
public MyThread(String name){
this.name =name;
}
public void run(){
for(int i =0;i<500;i++){
if(this.ticket>0){
System.out.println(this.name+"卖票---->"+(this.ticket--));
}
}
}
}
public static void main(String[] args) {
MyThread mt1= new MyThread("一号窗口");
MyThread mt2= new MyThread("二号窗口");
MyThread mt3= new MyThread("三号窗口");
mt1.start();
mt2.start();
mt3.start();
}
}
private int ticket =10;
private String name;
public void run(){
for(int i =0;i<500;i++){
if(this.ticket>0){
System.out.println(Thread.currentThread().getName()+"卖票---->"+(this.ticket--));
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//设计三个线程
MyThread1 mt = new MyThread1();
Thread t1 = new Thread(mt,"一号窗口");
Thread t2 = new Thread(mt,"二号窗口");
Thread t3 = new Thread(mt,"三号窗口");
//MyThread1 mt2 = new MyThread1();
//MyThread1 mt3 = new MyThread1();
t1.start();
t2.start();
t3.start();
}
作业一:删羊
package cn.edu.nuc.rxkassignment;
import java.util.ArrayList;
public class Test01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList goat=new ArrayList<>();
goat.add(new Goat("喜羊羊"));
goat.add(new Goat("懒羊羊"));
goat.add(new Goat("沸羊羊"));
goat.add(new Goat("慢羊羊"));
goat.add(new Goat("暖羊羊"));
/*for(int i=0;i<goat.size();i++){ //第一种删羊方法
goat.remove(goat.size()-1);
}*/
int len=goat.size();
/*for(int i=goat.size()-1;i>len-4;i--){ //第二种删羊方法:从末尾往前删
goat.remove(i);
}*/
for(int i=0;i<3;i++){
goat.remove(goat.size()-1); //第三种删羊方法:从末尾往前删除
}
for(Object s:goat){
System.out.println(((Goat)s).getName());
}
}
Iterator<Goat> iter=goat.iterator();//迭代器删羊方法
for(int i=0;i<3;i++){
iter.next();
}
iter.remove();
iter.next();
iter.remove();
iter.next();
iter.remove();
for(Object s:goat){
System.out.println(((Goat)s).getName());
}
class Goat{
private String name;
public Goat(){}
public Goat(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
package cn.edu.nuc.rxkassignment;
/* 创建线程的一种方式是:继承Thread类,并复写类中run方法 最后调用线程的start方法, 该方法启动线程并调用run方法(即和模板方法类似)
第二种方式是实现Runnable接口中的run方法
调用Thread(Runnable traget)构造函数实现
class ThreadDemo implements Runnable
{
public void run(){....}//将线程要运行的代码放在run中
}
实例时 ThreadDemo p =new ThreadDemo();
Thread t1 = new Thread(p);
两种方式的区别:
1.第一种方式继承了Thread类,但JAVA在类中只支持单继承,就不能继承其它类
继承Thread:线程代码存放在Thread子类run方法中
2.第二种方式实现了接口,但可以继续继承其它类;(避免了单继承局限性)
实现接口,代码存放在Runnable实现子类子的run方法中
*/
public class Test01{
public static void main(String[] args) {
//创建并启动继承线程
ThreadExtend t1=new ThreadExtend("一号");
ThreadExtend t2=new ThreadExtend("二号");
ThreadExtend t3=new ThreadExtend("三号");
t1.start();
t2.start();
t3.start();
//创建并启动实现接口线程
Thread i2=new Thread(new ThreadImple("2号"));
Thread i1=new Thread(new ThreadImple("1号"));
Thread i3=new Thread(new ThreadImple("3号"));
i1.start();
i2.start();
i3.start();
}
}
class ThreadImple implements Runnable{
public String name;
public ThreadImple(){}
public ThreadImple(String name){
this.name=name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<3;i++){
System.out.println("我是实现接口线程"+this.getName()+i);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;}
}
class ThreadExtend extends Thread{ //继承线程类
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<3;i++){
System.out.println("我是继承线程"+this.getName()+i);
}
}
}
package cn.edu.nuc.rxkassignment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.util.ArrayList;
public class Test01 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File folder=new File("F:"+File.separator+"Test"); //创建文件夹
if(!folder.exists()){
folder.mkdir();
}
File file=new File("F:"+File.separator+"test.txt");
for(int i=0;i<10;i++){ //创建10个记事本文件
file = new File(folder,"test"+i+".txt");
if(!file.exists()){
file.createNewFile();
}
String s="我真"+i;
FileOutputStream os=null;
os=new FileOutputStream(file);
os.write(s.getBytes());
os.close();
}
}
}
//方法1:从后往前
int len = sheepList.size();
for(int i = sheepList.size()-1; i >= len - 3; i--){
sheepList.remove(i);
}
//方法2:删除固定位置
for(int i = len - 3; i < len; i++){
sheepList.remove(len - 3);
}
//方法3:使用迭代器删除
int count = 0;
Iterator<Sheep> itr = sheepList.iterator();
while(itr.hasNext()){
if(count >= len - 3){
Object ed = itr.next();
itr.remove();
}else{
itr.next();
count++;
}
}
File manyFiles = new File("G:\\AndroidStudioProjects\\testForIO");
File file;
OutputStream os = null;
try {
for(int i = 0; i < 10; i++){
file = new File(manyFiles, "helloworld" + i + ".txt");
os = new FileOutputStream(file);
String s = "我真" + i;
os.write(s.getBytes());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Thread是类,Runnable是接口
* Runnable中没有start()方法,只有Thread才有,可以通过Thread类来启动Runnable子类实例
* 实现Runnable接口相比继承Thread类好处:1、避免继承的局限性,一个类可以有多个接口; 2、适合资源共享
* @author LiPeijing
*
*/
public class Actor extends Thread {
public void run(){
System.out.println(getName() + "是一个演员!");
int count = 0;
boolean keepRunning = true;
while(keepRunning){
System.out.println(getName() + "登台演出:" + (++count));
if(count == 100){
keepRunning = false;
}
if(count % 10 == 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(getName() + "的演出结束了!");
}
public static void main(String[] args){
Thread actor = new Actor();
actor.setName("Mr.Thread");
actor.start();
Thread actressThread = new Thread(new Actress(), "Ms.Runnable");
actressThread.start();
}
}
class Actress implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName() + "是一个演员!");
int count = 0;
boolean keepRunning = true;
while(keepRunning){
System.out.println(Thread.currentThread().getName() + "登台演出:" + (++count));
if(count == 100){
keepRunning = false;
}
if(count % 10 == 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName() + "的演出结束了!");
}
}
方法一
int k=sheepList.size();
for(int i=k-3;i<k;i++){
sheepList.remove(k-3);
}
方法二
int k=sheepList.size();
for(int i=k-1;i>=k-3;i--){
sheepList.remove(i);
}
方法三
for(int i=0;i<3;i++){
sheepList.remove(sheepList.size()-1);
}
方法四
int k=sheepList.size();
while(k-3<sheepList.size()){
Iterator itr=sheepList.iterator();
while(itr.hasNext()){
itr.next();
}
itr.remove();
}
for(int i=0;i<10;i++){
file=new File(floder,"HelloWorld"+i+".txt");
String s="我真"+i;
if(!file.exists())
file.createNewFile();
OutputStream os=new FileOutputStream(file);
os.write(s.getBytes());
os.close();
}
public static void main(String[] arge){
ThreadExtend th=new ThreadExtend("1--");
ThreadExtend th1=new ThreadExtend("2--");
ThreadExtend th2=new ThreadExtend("3--");
th.start();
th.start();
th1.start();
th2.start();
Thread th3=new Thread(new ThreadImpl("1--"));
Thread th4=new Thread(new ThreadImpl("2--"));
Thread th5=new Thread(new ThreadImpl("3--"));
th3.start();
th3.start();
th4.start();
th5.start();
}
}
class ThreadImpl implements Runnable{
private String name;
public ThreadImpl(String name){
this.name=name;
}
public ThreadImpl(){
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++){
System.out.println("i am"+this.getName()+i);
}
}
private String getName() {
// TODO Auto-generated method stub
return name;
}
}
class ThreadExtend extends Thread{
public ThreadExtend(String name){
super(name);
}
public ThreadExtend(){
super();
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++){
System.out.println("i am"+this.getName()+i);
}
}
Runnable与Thread的区别:Runnable可多次调用同一个线程对象,即可共享同一个线程对象,而Thread只能在同一对象的线程结束后才可再次调用,即不可共享同一个线程对象
方法1:
for (int i = 1; i <=sheepList.size(); i++) {
sheepList.remove(2);
}
方法2:
for(int i=sheepList.size()-1;i>1;i--){
sheepList.remove(i);
}
方法3:
System.out.println(sheepList.subList(0, 2));
for (int i = 0; i < sheepList.size(); i++) {
System.out.println(sheepList.get(i));
}
方法4:
Iterator it=sheepList.iterator();
while(it.hasNext()){
Sheep sheepList1=(Sheep)it.next();
if(sheepList1.getName()=="慢羊羊"||sheepList1.getName()=="沸羊羊"
||sheepList1.getName()=="懒羊羊"){
it.remove();
}
}
方法5:
int count=0;
int len=sheepList.size();
Iterator it=sheepList.iterator();
while(it.hasNext()){
if(count >= len - 3){
Object ed=it.next();
it.remove();
}
else{
it.next();
count++;
}
}
for(int i=0;i<10;i++){
File file3=new File(manyfile,"helloworld"+i+".txt");
file=new File(manyfile,"helloworld"+i+".txt");
OutputStream os=null;
try {
os=new FileOutputStream(file3);
String s="我真"+i;
try {
os.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
public class FirstThread extends Thread {
private int i;
public void run(){
for(i=0;i<5;i++){
System.out.println(getName()+" "+i);
}
}
public static void main(String[] args) {
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+""+i);
if(i==2){
new FirstThread().start();
new FirstThread().start();
}
}
}
}
public class SecondThread implements Runnable {
private int i;
public void run() {
for(i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+""+i);
if(i==2){
SecondThread st=new SecondThread();
new Thread(st,"新线程1").start();
new Thread(st,"新线程2").start();
}
}
}
}
Runnable和Thread的区别:
1,通过继承Thread类来创建线程,通过实现接口Runnable来创建线程类,一个类只能继承一个父 类,但可以实现多个接口,因此通过实现接口Runnable可以避免继承的局限性。
2,线程对象的创建:直接创建Thread子类即可代表线程对象;创建的Runable对象只能作为线程对象的target,多个线程可以共享同一个target,因此,实现接口Runnable适合于资源共享。
3,当线程类实现Runnable接口时,只能用Thread.currentThread()方法
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.go.donghao.linerlayout.MainActivity">
<LinearLayout
android:layout_weight="4"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="0dp">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="#ff0"
android:layout_height="match_parent" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="#fff"
android:layout_height="match_parent" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="#f00"
android:layout_height="match_parent" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:background="#00f"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#f0f"
android:layout_height="0dp" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#ff0"
android:layout_height="0dp" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.go.donghao.framelayout.MainActivity">
<TextView
android:layout_gravity="center"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#f0f"/>
<TextView
android:layout_gravity="center"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0"/>
<TextView
android:layout_gravity="center"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#0ff"/>
<TextView
android:layout_gravity="center"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#00f"/>
<TextView
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#fff"/>
<TextView
android:layout_gravity="center"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#000"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.go.donghao.rativatelayout.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正中"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/btn_zz" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/btn_zz"
android:text="正西"
android:id="@+id/btn_zx" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="正东"
android:layout_toRightOf="@id/btn_zz"
android:id="@+id/btn_zd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="正北"
android:layout_above="@id/btn_zz"
android:id="@+id/btn_zb" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="正南"
android:layout_below="@id/btn_zz"
android:id="@+id/btn_zn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="东北"
android:layout_toRightOf="@id/btn_zb"
android:layout_above="@id/btn_zd"
android:id="@+id/btn_db" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="西北"
android:layout_toLeftOf="@id/btn_zb"
android:layout_above="@id/btn_zx"
android:id="@+id/btn_xb" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="西南"
android:layout_toLeftOf="@id/btn_zn"
android:layout_below="@id/btn_zx"
android:id="@+id/btn_xn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="东南"
android:layout_toRightOf="@id/btn_zb"
android:layout_below="@id/btn_zd"
android:id="@+id/btn_dn" />
</RelativeLayout>
public static void main(String[] args) {
System.out.println("hello git");
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.