-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Description
1. 删羊
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++;
}
}
2. 批量写入文件
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();
}
}
}
3. Thread和Runnable区别
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels