-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava1.java
More file actions
594 lines (399 loc) · 13.1 KB
/
Java1.java
File metadata and controls
594 lines (399 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package pack1;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name, int age){
this.age = age;
this.name = name;
}
public int compareTo(Student student){
int num = this.age - student.age;
if(num == 0)
return this.name.compareTo(student.name);
return num;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public int hashCode(){
return age*3+name.hashCode();
}
public boolean equals(Object obj){
if(!(obj instanceof Student))
throw new ClassCastException("类型错误");
return this.name.equals(((Student) obj).name) && this.age == ((Student) obj).age;
}
}
/**
* Created by Root on 2016/7/19.
*/
/* interface NewInterface<T>{
public <T> void show(T t);
}
class Fanxing<T>{
private T obj;
public Fanxing(T obj){
this.obj = obj;
}
public void setObj(T obj){
this.obj = obj;
}
public T getObj(){
return obj;
}
}
class Demo <T> implements NewInterface<T>{
public<T> void show(T t){
Java1.printSomething(t);
}
}
class Person implements Comparable{
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
public int compareTo(Object obj){
if(!(obj instanceof Person))
throw new RuntimeException("Not Person");
Person p =(Person)obj;
if(p.getAge()-this.getAge() == 0)
return this.name.compareTo(p.getName());
return this.getAge()-p.getAge();
}
/*public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
return this.name.equals(((Person) obj).name)&& this.age == ((Person) obj).age;
}*/
/*
public int hashCode(){
return name.hashCode()+age*3;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
class MyCompare implements Comparator{
public int compare(Object o1,Object o2){
Person p1 = (Person)o1;
Person p2 = (Person)o2;
int comp = p1.getName().compareTo(p2.getName());
if(comp == 0)
return p1.getAge()-p2.getAge();
return comp;
}
}
*/
class stringCom implements Comparator<String>{
public int compare(String s1,String s2){
if(s1.length()-s2.length() == 0)
return s1.compareTo(s2);
return s1.length()-s2.length();
}
}
class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
class MyBufferedReader extends Reader{
private Reader fileReader;
MyBufferedReader(Reader fileReader){
this.fileReader = fileReader;
}
public int read(char []cbuf, int off ,int len) throws IOException{
return fileReader.read(cbuf,off,len);
}
public void close() throws IOException{
fileReader.close();
}
public String myReadLine() throws IOException{
StringBuilder stringBuilder = new StringBuilder();
int ch = 0;
while ((ch = fileReader.read()) != -1){
char chr = (char)ch;
if (chr == '\r' )
continue;
if (chr == '\n')
return stringBuilder.toString();
stringBuilder.append(chr);
}
if (stringBuilder.length() != 0)
return stringBuilder.toString();
return "";
}
}
class MyLineReader{
private FileReader fileReader;
MyLineReader(FileReader fileReader){
this.fileReader = fileReader;
}
public void myReadLineNum() throws IOException{
LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
String ch = null;
lineNumberReader.setLineNumber(100);
while ((ch = lineNumberReader.readLine()) != null){
Java1.printSomething(lineNumberReader.getLineNumber()+" : "+ch);
}
lineNumberReader.close();
}
}
class Solution1 {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Integer[] array = {1, 2, 3, -1, 0, -2, -3, -1};
Integer[][] store = new Integer[50][3];
Integer i, j, k, m = 0;
for (i = 0; i < array.length; i++)
for (j = i + 1; j < array.length; j++)
for (k = j + 1; k < array.length; k++) {
if (array[i] + array[j] + array[k] == 0) {
store[m][0] = array[i];
store[m][1] = array[j];
store[m][2] = array[k];
m++;
}
}
k = 0;
List<Integer> temp_list = new ArrayList<>();
for (i = 0; i < m; i++) {
Arrays.sort(store[i]);
for (j = i + 1; j < m; j++) {
Arrays.sort(store[j]);
if (!Arrays.equals(store[i], store[j]))
k++;
}
if (k == m - i - 1) {
Integer count_this = 0;
for (; count_this < 3; count_this++)
temp_list.add(store[i][count_this]);
list.add(temp_list);
temp_list.clear();
}
k = 0;
}
return list;
}
}
class MyFinalLineNumReader{
private int lineNumber = 0;
private Reader reader;
MyFinalLineNumReader(Reader reader){
this.reader = reader;
}
public int getLineNumber(){
return lineNumber;
}
public void setLineNumber(int lineNumber){
this.lineNumber = lineNumber;
}
public String myFinalLineReader() throws IOException{
lineNumber++;
StringBuilder stringBuilder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1){
char chr = (char)ch;
if (chr == '\r')
continue;
if (chr == '\n'){
return stringBuilder.toString();}
stringBuilder.append(chr);
}
if(stringBuilder.length() != 0)
return stringBuilder.toString();
return null;
}
public void myClose() throws IOException{
reader.close();
}
}
class LinkList{
abstract static class Node{
private Object firstNode;
private Object object;
private Node nextNode;
public void setFirstNode(Object firstNode){
this.firstNode = firstNode;
}
public void setNode(Object object,Node nextNode){
this.object = object;
this.nextNode = nextNode;
}
abstract public void getNode(Object object,Node nextNode);
}
}
public class Java1 {
public static void printSomething(Object obj) {
System.out.println(obj);
}
/*public static ArrayList getUniqueElem(ArrayList al){
ArrayList temp_al = new ArrayList();
Iterator it = al.iterator();
while (it.hasNext()){
Object obj = it.next();
if(!temp_al.contains(obj)){
temp_al.add(obj);
}
}
return temp_al;
}*/
static class stuComp implements Comparator<Student> {
public int compare(Student stu1, Student stu2) {
int num = stu1.getName().compareTo(stu2.getName());
if (num == 0)
return stu1.getAge() - stu2.getAge();
return num;
}
}
public static void showInfo(HashMap<String, String> hashMap) {
Set<Map.Entry<String, String>> set = hashMap.entrySet();
Iterator<Map.Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> temp = it.next();
printSomething(temp.getKey() + " .. " + temp.getValue());
}
}
public static void stringSort() {
TreeSet<String> list = new TreeSet<String>(Collections.reverseOrder(new stringCom()));
list.add("asd");
list.add("s22ds333333");
list.add("s23323");
list.add("abc");
}
public static void showArray(int... arr) {
for (int i : arr) {
printSomething(i);
}
}
public static void random() {
DecimalFormat dc = new DecimalFormat("0.00");
double getDouble = Double.parseDouble(dc.format(2.3292323));
printSomething(getDouble);
}
public static void writeFileStream() throws IOException{
FileOutputStream fostream = new FileOutputStream("demo.txt");
fostream.write("1234".getBytes());
fostream.close();
}
public static void readFileStream() throws IOException{
FileInputStream fistream = new FileInputStream("demo.txt");
byte buf[] = new byte[fistream.available()];
fistream.read(buf);
printSomething(new String(buf));
fistream.close();
}
public static void countBinaryOnes() {
int counter = 0,get_num ;
Scanner scanner = new Scanner(System.in);
//Scanner scanner2 = new Scanner(System.in);
get_num = scanner.nextInt();
String str = "12333334";
printSomething(str);
printSomething(get_num);
char[] array = str.toCharArray();
int[] int_array = new int[array.length];
for (int i = 0; i < int_array.length; i++) {
int_array[i] = (int) array[i];
}
int []count_array = new int[get_num];
for (int begin = 0,end = begin+get_num-1;end<int_array.length;begin++,end++){
for (int j=0,begin_inner = begin;j<get_num;j++,begin_inner++){
count_array[j] = int_array[begin_inner];
}
Arrays.sort(count_array);
char result = (char) (count_array[get_num-1]);
System.out.print(result+",");
}
}
public static void main(String[] args) throws IOException {
//countBinaryOnes();
String str = "aaabbv**accc**q****2";
String strings[] = str.split("[**]");
for (String s:
strings) {
if (!s.equals(""))
printSomething(s);
}
printSomething(strings.length);
/*showArray(2,3,4,5,1,2,3,6);
String [] strArr ={"123","232","2323"};
List <String>list = asList(strArr);
for(String tmp : list){
printSomething(tmp);
}
HashMap<Integer,String> hm = new HashMap<>();
hm.put(123,"222");
hm.put(124,"223");
hm.put(153,"222");
for(Map.Entry<Integer,String> me :hm.entrySet()){
printSomething(me.getKey()+" .. "+me.getValue());
}
/*writeFileStream();
readFileStream();
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("D:\\demo.txt");
fileWriter.write("fff");
fileWriter.write("mdzz");
}
catch (IOException ioe){
printSomething("IO异常");
}
finally {
try {
fileWriter.close();
}
catch (NullPointerException npe){
printSomething("空指针异常");
}
}
*/
/*HashMap<String,List<Student>> company = new HashMap<>();
List<Student> class1list = new ArrayList<>();
class1list.add(new Student("zhangsan",14));
class1list.add(new Student("lsis",14));
class1list.add(new Student("wangwu",14));
List<Student> class2list = new ArrayList<>();
class2list.add(new Student("zhangsan",18));
class2list.add(new Student("maliu",18));
class2list.add(new Student("zhangsan",18));
company.put("class1",class1list);
company.put("class2",class2list);
Set<Map.Entry<String,List<Student>>> entrySet = company.entrySet();
Iterator<Map.Entry<String,List<Student>>> iterator = entrySet.iterator();
while (iterator.hasNext()){
Map.Entry<String,List<Student>> temp = iterator.next();
printSomething("class = "+temp.getKey());
Iterator <Student> studentIterator = temp.getValue().iterator();
while (studentIterator.hasNext()){
Student tempStu = studentIterator.next();
printSomething(tempStu.getName()+" .. "+tempStu.getAge());
}
}*/
}
public static void copy_exe() throws IOException{
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\ccsetup509pro.exe"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\1121.exe"));
int ch = 0;
while ((ch = bufferedInputStream.read())!= -1){
bufferedOutputStream.write(ch);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
}