面试题12月28-积累篇

1、简述synchroized和java.util.concurrent.locks.Lock的异同?

1.synchronized 用在方法和代码块的区别?
     a. 可以只对需要同步的使用
     b.与wait(),notify()和notifyall()方法使用比较方便
2.wait()
    a。释放持有的对象锁,线程进入等待池,释放cpu,其他正在等待的线程可以获得锁,而sleep方法,线程会休眠一段时间,线程不会释放锁。
3.ReentrantLock
   还包括了中断锁等待和定时锁等待,
在并发量小的时候,用synchronize是比较好的选择,并发量大的时候用Lock。
synchronize是自动释放锁,Lock是主动释放锁
Lock可以设定所等待的时间,
有些操作不会发生冲突现象,需要用Lock解决,比如同时读文件。

2、Collerction框架中实现比较要实现什么接口?

Comparable和Comparator接口
如果一个类的不同对象需要比较大小,那么就需要实现这两个接口,根据业务需求定义规则。
使用区别:
(1)Comparable接口
是需要比较的类实现自己实现,例如:定义的Student类需要比较,需要Student类自己实现这个接口,实现ComParaTo()方法。
(2)Comparator接口
自己定义一个比较容器,去实现这个接口(实现compare方法),然后在集合生成的时候,用一个比较对象做参数,跟集合绑定。
例:
Class A implements Comparator{}//定义比较容器,实现compare方法
A a = new A();//生成比较对象
Set set = new TreeSet(a);//构造集合对象时,传入比较器。

3、java排序都有哪几种方法?请列举

  1 排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)
  2 用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等。
  3
  4 插入排序:
  5
  6 package org.rut.util.algorithm.support;
  7
  8 import org.rut.util.algorithm.SortUtil;
  9 /**
 10 * @author dongji
 11 * @since 2019-12-28
 12 * @version 1.0
 13 */
 14 public class InsertSort implements SortUtil.Sort{
 15
 16 /* (non-Javadoc)
 17 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
 18 */
 19 public void sort(int[] data) {
 20 int temp;
 21 for(int i=1;i<data.length;i++){
 22 for(int j=i;(j>0)&&(data[j]<data[j-1]);j–){
 23 SortUtil.swap(data,j,j-1);
 24 }
 25 }
 26 }
 27
 28 }
 29
 30 冒泡排序:
 31
 32 package org.rut.util.algorithm.support;
 33
 34 import org.rut.util.algorithm.SortUtil;
 35
 36 /**
 37 * @author dongji
 38 * @since 2019-12-28
 39 * @version 1.0
 40 */
 41 public class BubbleSort implements SortUtil.Sort{
 42
 43 /* (non-Javadoc)
 44 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
 45 */
 46 public void sort(int[] data) {
 47 int temp;
 48 for(int i=0;i<data.length;i++){
 49 for(int j=data.length-1;j>i;j–){
 50 if(data[j]<data[j-1]){
 51 SortUtil.swap(data,j,j-1);
 52 }
 53 }
 54 }
 55 }
 56
 57 }
 58
 59 选择排序:
 60
 61 package org.rut.util.algorithm.support;
 62
 63 import org.rut.util.algorithm.SortUtil;
 64
 65 /**
 66 * @author dongji
 67 * @since 2019-12-28
 68 * @version 1.0
 69 */
 70 public class SelectionSort implements SortUtil.Sort {
 71
 72 /*
 73 * (non-Javadoc)
 74 *
 75 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
 76 */
 77 public void sort(int[] data) {
 78 int temp;
 79 for (int i = 0; i < data.length; i++) {
 80 int lowIndex = i;
 81 for (int j = data.length – 1; j > i; j–) {
 82 if (data[j] < data[lowIndex]) {
 83 lowIndex = j;
 84 }
 85 }
 86 SortUtil.swap(data,i,lowIndex);
 87 }
 88 }
 89
 90 }
 91
 92 Shell排序:
 93
 94 package org.rut.util.algorithm.support;
 95
 96 import org.rut.util.algorithm.SortUtil;
 97
 98 /**
 99 * @author dongji
100 * @since 2019-12-28
101 * @version 1.0
102 */
103 public class ShellSort implements SortUtil.Sort{
104
105 /* (non-Javadoc)
106 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
107 */
108 public void sort(int[] data) {
109 for(int i=data.length/2;i>2;i/=2){
110 for(int j=0;j<i;j++){
111 insertSort(data,j,i);
112 }
113 }
114 insertSort(data,0,1);
115 }
116
117 /**
118 * @param data
119 * @param j
120 * @param i
121 */
122 private void insertSort(int[] data, int start, int inc) {
123 int temp;
124 for(int i=start+inc;i<data.length;i+=inc){
125 for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
126 SortUtil.swap(data,j,j-inc);
127 }
128 }
129 }
130
131 }
132
133 快速排序:
134
135 package org.rut.util.algorithm.support;
136
137 import org.rut.util.algorithm.SortUtil;
138
139 /**
140 * @author dongji
141 * @since 2019-12-28
142 * @version 1.0
143 */
144 public class QuickSort implements SortUtil.Sort{
145
146 /* (non-Javadoc)
147 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
148 */
149 public void sort(int[] data) {
150 quickSort(data,0,data.length-1);
151 }
152 private void quickSort(int[] data,int i,int j){
153 int pivotIndex=(i+j)/2;
154 //swap
155 SortUtil.swap(data,pivotIndex,j);
156
157 int k=partition(data,i-1,j,data[j]);
158 SortUtil.swap(data,k,j);
159 if((k-i)>1) quickSort(data,i,k-1);
160 if((j-k)>1) quickSort(data,k+1,j);
161
162 }
163 /**
164 * @param data
165 * @param i
166 * @param j
167 * @return
168 */
169 private int partition(int[] data, int l, int r,int pivot) {
170 do{
171 while(data[++l]<pivot);
172 while((r!=0)&&data[--r]>pivot);
173 SortUtil.swap(data,l,r);
174 }
175 while(l<r);
176 SortUtil.swap(data,l,r);
177 return l;
178 }
179
180 }
181
182 改进后的快速排序:
183
184 package org.rut.util.algorithm.support;
185
186 import org.rut.util.algorithm.SortUtil;
187
188 /**
189 * @author dongji
190 * @since 2019-12-28
191 * @version 1.0
192 */
193 public class ImprovedQuickSort implements SortUtil.Sort {
194
195 private static int MAX_STACK_SIZE=4096;
196 private static int THRESHOLD=10;
197 /* (non-Javadoc)
198 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
199 */
200 public void sort(int[] data) {
201 int[] stack=new int[MAX_STACK_SIZE];
202
203 int top=-1;
204 int pivot;
205 int pivotIndex,l,r;
206
207 stack[++top]=0;
208 stack[++top]=data.length-1;
209
210 while(top>0){
211 int j=stack[top--];
212 int i=stack[top--];
213
214 pivotIndex=(i+j)/2;
215 pivot=data[pivotIndex];
216
217 SortUtil.swap(data,pivotIndex,j);
218
219 //partition
220 l=i-1;
221 r=j;
222 do{
223 while(data[++l]<pivot);
224 while((r!=0)&&(data[--r]>pivot));
225 SortUtil.swap(data,l,r);
226 }
227 while(l<r);
228 SortUtil.swap(data,l,r);
229 SortUtil.swap(data,l,j);
230
231 if((l-i)>THRESHOLD){
232 stack[++top]=i;
233 stack[++top]=l-1;
234 }
235 if((j-l)>THRESHOLD){
236 stack[++top]=l+1;
237 stack[++top]=j;
238 }
239
240 }
241 //new InsertSort().sort(data);
242 insertSort(data);
243 }
244 /**
245 * @param data
246 */
247 private void insertSort(int[] data) {
248 int temp;
249 for(int i=1;i<data.length;i++){
250 for(int j=i;(j>0)&&(data[j]<data[j-1]);j–){
251 SortUtil.swap(data,j,j-1);
252 }
253 }
254 }
255
256 }
257
258 归并排序:
259
260 package org.rut.util.algorithm.support;
261
262 import org.rut.util.algorithm.SortUtil;
263
264 /**
265 * @author dongji
266 * @since 2019-12-28
267 * @version 1.0
268 */
269 public class MergeSort implements SortUtil.Sort{
270
271 /* (non-Javadoc)
272 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
273 */
274 public void sort(int[] data) {
275 int[] temp=new int[data.length];
276 mergeSort(data,temp,0,data.length-1);
277 }
278
279 private void mergeSort(int[] data,int[] temp,int l,int r){
280 int mid=(l+r)/2;
281 if(l==r) return ;
282 mergeSort(data,temp,l,mid);
283 mergeSort(data,temp,mid+1,r);
284 for(int i=l;i<=r;i++){
285 temp[i]=data[i];
286 }
287 int i1=l;
288 int i2=mid+1;
289 for(int cur=l;cur<=r;cur++){
290 if(i1==mid+1)
291 data[cur]=temp[i2++];
292 else if(i2>r)
293 data[cur]=temp[i1++];
294 else if(temp[i1]<temp[i2])
295 data[cur]=temp[i1++];
296 else
297 data[cur]=temp[i2++];
298 }
299 }
300
301 }
302
303 改进后的归并排序:
304
305 package org.rut.util.algorithm.support;
306
307 import org.rut.util.algorithm.SortUtil;
308
309 /**
310 * @author dongji
311 * @since 2019-12-28
312 * @version 1.0
313 */
314 public class ImprovedMergeSort implements SortUtil.Sort {
315
316 private static final int THRESHOLD = 10;
317
318 /*
319 * (non-Javadoc)
320 *
321 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
322 */
323 public void sort(int[] data) {
324 int[] temp=new int[data.length];
325 mergeSort(data,temp,0,data.length-1);
326 }
327
328 private void mergeSort(int[] data, int[] temp, int l, int r) {
329 int i, j, k;
330 int mid = (l + r) / 2;
331 if (l == r)
332 return;
333 if ((mid – l) >= THRESHOLD)
334 mergeSort(data, temp, l, mid);
335 else
336 insertSort(data, l, mid – l + 1);
337 if ((r – mid) > THRESHOLD)
338 mergeSort(data, temp, mid + 1, r);
339 else
340 insertSort(data, mid + 1, r – mid);
341
342 for (i = l; i <= mid; i++) {
343 temp[i] = data[i];
344 }
345 for (j = 1; j <= r – mid; j++) {
346 temp[r - j + 1] = data[j + mid];
347 }
348 int a = temp[l];
349 int b = temp[r];
350 for (i = l, j = r, k = l; k <= r; k++) {
351 if (a < b) {
352 data[k] = temp[i++];
353 a = temp[i];
354 } else {
355 data[k] = temp[j--];
356 b = temp[j];
357 }
358 }
359 }
360
361 /**
362 * @param data
363 * @param l
364 * @param i
365 */
366 private void insertSort(int[] data, int start, int len) {
367 for(int i=start+1;i<start+len;i++){
368 for(int j=i;(j>start) && data[j]<data[j-1];j–){
369 SortUtil.swap(data,j,j-1);
370 }
371 }
372 }
373
374 }
375
376 堆排序:
377
378 package org.rut.util.algorithm.support;
379
380 import org.rut.util.algorithm.SortUtil;
381
382 /**
383 * @author dongji
384 * @since 2019-12-28
385 * @version 1.0
386 */
387 public class HeapSort implements SortUtil.Sort{
388
389 /* (non-Javadoc)
390 * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
391 */
392 public void sort(int[] data) {
393 MaxHeap h=new MaxHeap();
394 h.init(data);
395 for(int i=0;i<data.length;i++)
396 h.remove();
397 System.arraycopy(h.queue,1,data,0,data.length);
398 }
399
400 private static class MaxHeap{
401
402 void init(int[] data){
403 this.queue=new int[data.length+1];
404 for(int i=0;i<data.length;i++){
405 queue[++size]=data[i];
406 fixUp(size);
407 }
408 }
409
410 private int size=0;
411
412 private int[] queue;
413
414 public int get() {
415 return queue[1];
416 }
417
418 public void remove() {
419 SortUtil.swap(queue,1,size–);
420 fixDown(1);
421 }
422 //fixdown
423 private void fixDown(int k) {
424 int j;
425 while ((j = k << 1) <= size) {
426 if (j < size && queue[j]<queue[j+1])
427 j++;
428 if (queue[k]>queue[j]) //不用交换
429 break;
430 SortUtil.swap(queue,j,k);
431 k = j;
432 }
433 }
434 private void fixUp(int k) {
435 while (k > 1) {
436 int j = k >> 1;
437 if (queue[j]>queue[k])
438 break;
439 SortUtil.swap(queue,j,k);
440 k = j;
441 }
442 }
443
444 }
445
446 }
447
448 SortUtil:
449
450 package org.rut.util.algorithm;
451
452 import org.rut.util.algorithm.support.BubbleSort;
453 import org.rut.util.algorithm.support.HeapSort;
454 import org.rut.util.algorithm.support.ImprovedMergeSort;
455 import org.rut.util.algorithm.support.ImprovedQuickSort;
456 import org.rut.util.algorithm.support.InsertSort;
457 import org.rut.util.algorithm.support.MergeSort;
458 import org.rut.util.algorithm.support.QuickSort;
459 import org.rut.util.algorithm.support.SelectionSort;
460 import org.rut.util.algorithm.support.ShellSort;
461
462 /**
463 * @author dongji
464 * @since 2019-12-28
465 * @version 1.0
466 */
467 public class SortUtil {
468 public final static int INSERT = 1;
469 public final static int BUBBLE = 2;
470 public final static int SELECTION = 3;
471 public final static int SHELL = 4;
472 public final static int QUICK = 5;
473 public final static int IMPROVED_QUICK = 6;
474 public final static int MERGE = 7;
475 public final static int IMPROVED_MERGE = 8;
476 public final static int HEAP = 9;
477
478 public static void sort(int[] data) {
479 sort(data, IMPROVED_QUICK);
480 }
481 private static String[] name={
482 “insert”, “bubble”, “selection”, “shell”, “quick”, “improved_quick”, “merge”, “improved_merge”, “heap”
483 };
484
485 private static Sort[] impl=new Sort[]{
486 new InsertSort(),
487 new BubbleSort(),
488 new SelectionSort(),
489 new ShellSort(),
490 new QuickSort(),
491 new ImprovedQuickSort(),
492 new MergeSort(),
493 new ImprovedMergeSort(),
494 new HeapSort()
495 };
496
497 public static String toString(int algorithm){
498 return name[algorithm-1];
499 }
500
501 public static void sort(int[] data, int algorithm) {
502 impl[algorithm-1].sort(data);
503 }
504
505 public static interface Sort {
506 public void sort(int[] data);
507 }
508
509 public static void swap(int[] data, int i, int j) {
510 int temp = data[i];
511 data[i] = data[j];
512 data[j] = temp;
513 }
514 }

常见的java的排序方法

4、为什么要用ORM?它和JDBC有何不一样?

ORM是一种思想,就是把object转变成数据库中的记录,或者把数据库中的记录转变成为object,我们可以用JDBC来实现这个思想,其实,如果我们的项目是严格按照oop方式编写的话,我们的JDBC程序不管是有意还是无意,就已经实现ORM的工作了。

现在有许多ORM工具,它们底层调用JDBC来实现ORM工作,我们直接使用这些工具,就省去了之间使用JDBC的繁琐细节,提高了开发效率,现在用的较多的ORM工具是hibernate。也听说一谢其他ORM工具,如toplink,ojb等。

5、B/S与C/S的联系与区别。

C/S 是Client/Server 的缩写。服务器通常采用高性能的PC、工作站或小型机,并采用
大型数据库系统,如Oracle、Sybase、Informix 或SQL Server。客户端需要安装专用的客户
端软件。
B/S是Brower/Server 的缩写,客户机上只要安装一个浏览器(Browser),如Netscape
Navigator 或Internet Explorer,服务器安装Oracle、Sybase、Informix 或SQL Server 等数据
库。在这种结构下,用户界面完全通过WWW 浏览器实现,一部分事务逻辑在前端实现,
但是主要事务逻辑在服务器端实现。浏览器通过Web Server 同数据库进行数据交互。
C/S 与B/S 区别:
1.硬件环境不同:
C/S 一般建立在专用的网络上, 小范围里的网络环境, 局域网之间再通过专门服
务器提供连接和数据交换服务.
B/S 建立在广域网之上的, 不必是专门的网络硬件环境,例与电话上网, 租用设备.
信息自己管理. 有比C/S更强的适应范围, 一般只要有操作系统和浏览器就行
2.对安全要求不同
C/S 一般面向相对固定的用户群, 对信息安全的控制能力很强. 一般高度机密的
信息系统采用C/S 结构适宜. 可以通过B/S发布部分可公开信息.
B/S 建立在广域网之上, 对安全的控制能力相对弱, 可能面向不可知的用户。
3.对程序架构不同
C/S 程序可以更加注重流程, 可以对权限多层次校验, 对系统运行速度可以较少
考虑.
B/S 对安全以及访问速度的多重的考虑, 建立在需要更加优化的基础之上. 比C/S
有更高的要求B/S 结构的程序架构是发展的趋势, 从MS 的.Net 系列的BizTalk 2000
Exchange 2000 等, 全面支持网络的构件搭建的系统. SUN 和IBM推的JavaBean 构件技术
等,使B/S更加成熟.
4.软件重用不同
C/S 程序可以不可避免的整体性考虑, 构件的重用性不如在B/S 要求下的构件的
重用性好.
B/S 对的多重结构,要求构件相对独立的功能. 能够相对较好的重用.就入买来的餐
桌可以再利用,而不是做在墙上的石头桌子
5.系统维护不同
C/S 程序由于整体性, 必须整体考察, 处理出现的问题以及系统升级. 升级难. 可
能是再做一个全新的系统
B/S 构件组成,方面构件个别的更换,实现系统的无缝升级. 系统维护开销减到最小.
用户从网上自己下载安装就可以实现升级.
6.处理问题不同
C/S 程序可以处理用户面固定, 并且在相同区域, 安全要求高需求, 与操作系统相
关. 应该都是相同的系统
B/S 建立在广域网上, 面向不同的用户群, 分散地域, 这是C/S 无法作到的. 与操
作系统平台关系最小.
7.用户接口不同
C/S 多是建立的Window平台上,表现方法有限,对程序员普遍要求较高
B/S 建立在浏览器上, 有更加丰富和生动的表现方式与用户交流. 并且大部分难
度减低,减低开发成本.
8.信息流不同
C/S 程序一般是典型的中央集权的机械式处理, 交互性相对低
B/S 信息流向可变化, B-B B-C B-G 等信息、流向的变化, 更像交易中心。

6、Spring框架中都用到了哪些设计模式?

数据库词典40-60

原文地址:https://www.cnblogs.com/blackboc-java/p/12111593.html

时间: 2024-08-27 03:07:41

面试题12月28-积累篇的相关文章

【干货】2016年12月28日 阿里云内部技术分享

高并发IM系统架构优化实践http://click.aliyun.com/m/8768/老司机推荐:云端建站10分钟快速上手教程http://click.aliyun.com/m/8769/万亿user_tags级实时推荐系统数据库设计http://click.aliyun.com/m/8770/新生代程序猿,如何进入心仪女神的科技大公司就职呢?http://click.aliyun.com/m/8771/是不是硬件上云就叫智能硬件?http://click.aliyun.com/m/8772/

阮一峰网络日志 第37期 2018年12月28日

http://www.ruanyifeng.com/blog/2018/12/weekly-issue-37.html 声明:链接及文章内容为原博主阮一峰原创. 原文地址:https://www.cnblogs.com/sanen/p/10242378.html

winform与面向对象应用做一个计算器12月28日

代码部分: public partial class 计算器 : Form { public 计算器() { InitializeComponent(); } private double sum = 0;//存放运算结果 private string biaodashi="";//用于存放除了刚点过的运算符的前面表达式部分 private string preyunsuanfu="";//用来存放上一步运算符,用于下次点运算符的时候算上一步的结果 private

12月28日 进制转换的另种方法

以……8  4  2  1 为运算数组 1.十进制转二进制 如以十进制98为例,转换成二进制. 参考数组 128 64 32 16 8 4 2 1 98可以去掉1个64,余下34,34可以去掉1个32余下2 ,2可以去掉0个16,0个8 ,0个4,2可以去掉1个2,余下0,0可以去掉0个1. 所以  64  32  16  8  4  2  1 1    1   0   0  0  1   0 2. 二进制转十进制 110011001 1         1        0     0    

12月28 数组的几种应用(冒泡、折半查找)

*************数组的应用************* 一.冒泡排序(升序.降序) 1.双层循环(循环套循环) (1).冒泡排序是用双层循环解决.外层循环的是趟数,里层循环的是次数.(2).趟数=n-1:次数=n-趟数.(3).里层循环使用if比较相临的两个数的大小,进行数值交换. 二.折半查找(也叫二分法) 1.前提:数组必须有序. 2.主要就是3个未知量. 顶部:topsub 底部:bottomsub 中间:midsub =(topsub+bottomsub)/2 将数组一分为二,然

12月28日 二维数组的应用:第一个小游戏(推箱子)

小游戏:******推箱子******** static void Main(string[] args) { int i, j; int[,] a = new int[10, 10]                  //二维数组的定义           类型[,] 数组名 = new  类型 [行数, 列数] {赋值}:   或单个赋值 a[i,j]=1; { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,8,0,0,0,

12月28日作业

1. 十六进制4CD5.B3转成八进制. 先将十六进制4CD5.B3转换成二进制,结果为:100110011010101.11010011   在将所得二进制转化成八进制 结果为:46325.646 2.八进制25671.23转成十六进制. 先将八进制25671.23转换成二进制,结果为:10101110111001.010011 再将所得二进制转化成十六进制  结果为:2BB9.4B

关于12月28日到12月29日

无奈C++大作业还没写.而且同学说大作业要写界面.渣渣不会MFC.默默地写了两天OpenCV 上图吧. 1.题目 2.主界面 3.游戏说明 4.注册界面 5.登录界面 6.关卡 7.得分 8.排行榜 嘿嘿.萌吧.^V^

米的建站日记(2014年12月28日)

正则替换换行符和把 br 替换成换行符 dxycode=dxycode.replace(/<br\s*\/?>/gi,"\r\n"); 来源于:http://www.jb51.net/article/51075.htm 记录一下,以后方便查找. <br/> --------------分割线-------------- <br/> 今天终于做出决定了,偶先不做后台了,现在后台加上数据库弄晕我了.~  先模拟前后台用json进行交互,如