java基础代码练习 下载本文

内容发布更新时间 : 2024/11/16 20:49:42星期一 下面是文章的全部内容请认真阅读。

多线程:

需求:简单的买票程序;多个窗口同时买票。

class Ticket extends Thread{ private staticint tick=100; public void run(){ while(true){ if(tick>0)

System.out.println(Thread.currentThread().getName()+\ } } }

class TicketDemo{

public static void main(String[]args){ Ticket t1=new Ticket(); Ticket t2=new Ticket(); Ticket t3=new Ticket(); Ticket t4=new Ticket(); t1.start(); t2.start(); t3.start(); t4.start(); } }

class Ticket implements Runnable{ private int tick=100; public void run(){ while(true){ if(tick>0){

System.out.println(Thread.currentThread().getName()+\ } } } }

class TicketDemo{

public static void main(String[]args){ Ticket t = new Ticket();

Thread t1=new Thread(t);//创建了线程; Thread t2=new Thread(t); Thread t3=new Thread(t); Thread t4=new Thread(t);

t1.start(); t2.start(); t3.start(); t4.start();

} } “==”和equals 区别:和数组小练习: public class Ha{ int x; Ha(int x){ this.x=x; }

/** 比较两个Ha对象的x值是否一样

a.equals(b)中的a由this来代表,b是obj参数, b.equals(a)中的b由this来代表,a是obj参数; 比较两个人的身份证是否一样 */

public boolean equals(Object obj){ return (this.x==((Ha)obj).x); } }

class Main{

public static void main(String[]args){ // Ha a=new Ha(); // Ha b=a ;

// Ha a =new Ha(20); // Ha b =new Ha(20); // //

// System.out.println(a); // System.out.println(b);

// System.out.println(a==b);

// System.out.println(a.equals(b));

int [][] a={ {1,2,3}, {3,4},

{4,5,6}

};

for (int i=0;i

for (int j=0;j

} } } } 集合: import java.util.*;

class CollectionDemo01{

public static void main(String []args){ ArrayList al=new ArrayList(); al.add(\ al.add(\ al.add(\ al.add(\ sop(al);

// al.remove(\ sop(al.contains(\ sop(\ sop(al); }

public static void sop(Object obj){ System.out.println(obj); } } 计算器: public class Calculator {

static int calc(int a,int b,String operator) throws Exception {

if(operator.equals(\ return a+b;}

else if(operator.equals(\