Java编程

牛客网java错题总结一

关注赞赏支持牛客网 Java专项练习 选择题错题 [1]1. java中Hashtable, Vector, TreeSet, LinkedList哪些线程是安全的?HashTable Vector。

抽奖牛客网java专项练习错题本第四单元关注赞赏支持牛客网java专项练习错题本前言这是一篇打算持续更新的文章。最近在刷牛课网上的专项练习——JAVA部分。感觉对复习一些基础知识、原先模糊的部分还是很有帮助的。为了加深印象和方便日后复习。在这里记录下一些做错的题和虽然做对但觉得有必要记录下的题目。

牛客网java错题总结一

x,y,z 分别占用3,4,5 位,int是4个字节32位,相当于xyz 占用4个字节,double 占8个字节,按照对齐原则,前面补4位,4+4+8=16  。并且要知道字节对齐有原则就是最后所占用的字节数要是其中最大内存数据类型的整数倍。

/ 保存线程池状态和工作线程数:* 低29位: 工作线程数* 高3位 : 线程池状态*/private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));private static final int COUNT_BITS = Integer.SIZE – 3;// 最大线程数: 2^29-1private static final int CAPACITY = (1 << COUNT_BITS) - 1; // 00011111 11111111 11111111 11111111// 线程池状态// 11100000 00000000 00000000 00000000private static final int RUNNING = -1 << COUNT_BITS;// 00000000 00000000 00000000 00000000private static final int SHUTDOWN = 0 << COUNT_BITS;// 00100000 00000000 00000000 00000000private static final int STOP = 1 << COUNT_BITS;// 01000000 00000000 00000000 00000000private static final int TIDYING = 2 << COUNT_BITS;// 01100000 00000000 00000000 00000000private static final int TERMINATED = 3 << COUNT_BITS;可以看到,ThreadPoolExecutor一共定义了5种线程池状态:

//线程工厂public class TaskThreadFactory implements ThreadFactory {private final AtomicInteger mThreadNumber = new AtomicInteger(1);private final String mNamePrefix;TaskThreadFactory(String name) {mNamePrefix = name + “#”;}public Thread newThread(Runnable r) {Thread t = new Thread(r,mNamePrefix + mThreadNumber.getAndIncrement());// if (t.isDaemon())// t.setDaemon(false);//// if (t.getPriority() != Thread.NORM_PRIORITY)// t.setPriority(Thread.NORM_PRIORITY);return t;}}//重写runnablepublic class PRunnable implements Runnable {public static final int HIGH = 1;//优先级高public static final int NORMAL = 2;//优先级中等public static final int LOW = 3;//优先级低@IntDef({HIGH,NORMAL,LOW})@Retention(RetentionPolicy.SOURCE)public @interface Priority{}public final int priority;private final Runnable runnable;public int serial;public PRunnable(Runnable runnable){this(NORMAL,runnable);}public PRunnable(@Priority int priority,Runnable runnable){this.priority = priority;this.runnable = runnable;}@Overridepublic void run() {if (runnable != null) {runnable.run();}}/ 线程队列方式 先进先出* @param r1* @param r2* @return*/public static final int compareFIFO(PRunnable r1, PRunnable r2) {int result = r1.priority-r2.priority;return result==0?r1.serial-r2.serial:result;}/ 线程队列方式 后进先出* @param r1* @param r2* @return*/public static final int compareLIFO(PRunnable r1, PRunnable r2) {int result = r1.priority-r2.priority;return result==0?r2.serial-r1.serial:result;}}//线程池实现public class TaskExecutor implements Executor {private final static int QUEUE_INIT_CAPACITY = 20;private static final int CORE = 3;private static final int MAX = 5;private static final int TIMEOUT = 30 * 1000;private AtomicInteger SERIAL = new AtomicInteger(0);//主要获取添加任务public static class Config {public int core;public int max;public int timeout;public boolean allowCoreTimeOut;public boolean fifo;public Config(int core, int max, int timeout, boolean allowCoreTimeOut,boolean fifo) {this.core = core;this.max = max;this.timeout = timeout;this.allowCoreTimeOut = allowCoreTimeOut;this.fifo = fifo;}}public static Config defaultConfig = new Config(CORE, MAX, TIMEOUT, true,true);private final String name;private final Config config;private ExecutorService service;public TaskExecutor(String name) {this(name, defaultConfig);}public TaskExecutor(String name, Config config) {this(name, config, true);}public TaskExecutor(String name, Config config, boolean startup) {this.name = name;this.config = config;if (startup) {startup();}}public void startup() {synchronized (this) {if (service != null && !service.isShutdown()) {return;}service = createExecutor(config);}}public void shutdown() {ExecutorService executor = null;synchronized (this) {// 交换变量if (service != null) {executor = service;service = null;}}if (executor != null) {// 停止线程if (!executor.isShutdown()) {executor.shutdown();}// 回收变量executor = null;}}private void executeRunnable(PRunnable runnable) {synchronized (this) {if (service == null。

b<0) throw new NegativeIntegerException(); int y=0; int x=getMaxCommonDivisor(a,b); y=(a*b)/x; return y; } } public class Example{ public static void main (String arg[]){ int maxCommonDivisor, minCommonMultiple; MaxCommonDivisor max=new MaxCommonDivisor(); MinCommonMultiple min = new MinCommonMultiple(); try{ maxCommonDivisor=max.getMaxCommonDivisor(18,12); System.out.println("最大公约数: "+ maxCommonDivisor); minCommonMultiple=min.getMinCommonMultiple(18,12); System.out.println("最小公倍数: "+minCommonMultiple); maxCommonDivisor=max.getMaxCommonDivisor(-64,48); System.out.println("最大公约数: "+maxCommonDivisor); } catch(NegativeIntegerException e){ System.out.println(e.toString()); } } } 3。编写一个类要求实现一个接口,该接口有 3 个 abstract 方法: Public abstract void f( int x); Public abstract void g(int x, int y); Public abstract double h(double x); 要求在应用程序主类中使用该类创建对象,并使用接口回调调用方法 f(), g() 和 h(). 解: interface A{public abstract void f(int x); public abstract void g(int x,int y); public abstract double h(double x); } class A1 implements A{ public void f(int x){ System.out.println(x); } public void g(int x,int y){ int z=x+y; System.out.println(z); } public double h(double x){ return x*x; } } class A2 implements A{ public void f(int x){ System.out.println("Hello:"+x); } public void g(int x,int y){ int z=x-y; System.out.println(z); } public double h(double x){ return Math.sqrt(x); } } class A3 implements A{ public void f(int x){ System.out.println("你好:"+x); } public void g(int x,int y){ double z=(double)x/y; System.out.println(z); } public double h(double x){ return 1/x; } } public class ZuoYe5_3{ public static void main(String args[ ]){ A a=new A1(); a.f(10);a.g(12,20); System.out.println(a.h(100)); a=new A2(); a.f(10); a.g(12,20); System.out.println(a.h(100)); a=new A3(); a.f(10); a.g(12,20); System.out.println(a.h(100)); } } 利用面向接口编程的方法, 编写计算锥形体积的完整程序, 注意锥形的底可以为圆形, 矩形, 或梯形等。 (对细节满足开闭原则。 ) 4 求下列程序运行结果: class A{ public double y=11.456789; public void f(){ y=y+1; System.out.printf("y 是 double 型的变量,y=%f\n",y); } } class B extends A{ int y=0; public void g(){ y=y+100; System.out.printf("y 是 int 型的变量 ,y=%d\n",y); } } class Example5_3{ public static void main(String args[ ]){ B b=new B(); b.y=200; b.g(); b.f(); } } // 调用子类新增的方法 //调用子类继承的方法5.求下列程序运行结果 class A{ int m=0,n=0; long f(){ return m+n; } } class B extends A{ int m=1,n=1; long f(){ long result=0; super.m=10; super.n=20; result=super.f()+(m+n); return result; } long g(){ long result=0; result=super.f(); return result/2; } } public class Example5_7{ public static void main(String args[ ]){ B b=new B(); b.m=3; b.n=7; long resultOne=b.g(); long resultTwo=b.f(); long resultThree=b.g(); System.out.println("resultOne="+resultOne); System.out.println("resultTwo="+resultTwo); System.out.println("resultThree="+resultThree); } }(6) 即所谓的开闭原则,对_________关闭,对______________开放。 答: 修改,扩展 (7)编写一个面向抽象的完整程序,求柱形体积,计算方法底*高,但底面积的算法经常要 变化。于是对于求底面积可以设一个抽象类 Geometry 。柱形面对具有抽象类的 Geometry 设计。最后完成对圆形底面积的柱形,和梯形底面积的柱形计算体积。 (8)求下列程序的运行结果。 interface Show{ void show(); } class A implements Show{ public void show(){ System.out.println("I love This Game"); } } class B implements Show{ public void show(){ System.out.println("我喜欢看 NBA"); } } class C{ public void f(Show s){ s.show(); } // 接口作为参数} public class Example5_13{ public static void main(String args[]){ C c=new C(); c.f(new A()); c.f(new B()); } } 答: I love This Game 我喜欢看 NBA (9)写出下面程序的运行结果: public class Example5_18{ public static void main(String args[ ]){ int n=0,m=0,t=0;try{ t=9999; m=Integer.parseInt("8888"); n=Integer.parseInt("12s3a"); //发生异常,转向 catch System.out.println("我没有机会输出 "); } catch(Exception e){ System.out.println("发生异常 "); n=123; } System.out.println("n="+n+",m="+m+",t="+t); } } 答:发生异常 n=123, m=8888, t=9999第六章 字符串和正则表达式1。对于字符串 String S1=new String(“ OK”); String S2=new String(“ OK”); 说出下列表达式的值: S1==S2 S1.equals(S2) 答: (1) false (2) true 2.对于字符串 String s1=new String(“I love you zhht”); String s2=s1.replaceAll(“love” ,”hate ”); 说出 System.out.printf(“%s, %s”, s1,s2)的结果。 结果:I love。

public final int getAndAdd(int delta) //获取当前的值,并加上预期的值。

package com.badlogic.androidgames.framework;import java.util.List;public interface Input { public static class KeyEvent { public static final int KEY_DOWN = 0; public static final int KEY_UP = 1; public int type; public int keyCode; public char keyChar; } public static class TouchEvent { public static final int TOUCH_DOWN = 0; public static final int TOUCH_UP = 1; public static final int TOUCH_DRAGGED = 2; public int type; public int x, y; public int pointer; } public boolean isKeyPressed(int keyCode); public boolean isTouchDown(int pointer); public int getTouchX(int pointer); public int getTouchY(int pointer); public float getAccelX(); public float getAccelY(); public float getAccelZ(); public List getKeyEvents(); public List getTouchEvents();}上述定义包括两个静态类:KeyEvent 和 TouchEvent。KeyEvent类和TouchEvent类都定义了相关事件的常量。KeyEvent同时定义了几个存储事件信息的变量:类型(type)、按键码(keyCode)和Unicode字符(keyChar)。TouchEvent也一样,定义了位置信息(x,y)、某个出触摸点的ID。比如第一个手指按下,ID是0,第二个手指按下 ID是1;如果两个手指按下,手指0释放,手指1保持,又一个手指按下时,ID为已经释放的0。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注