1. ArrayList去除集合中字符串的重复值
package com. guada. list;
import java. util. ArrayList;
import java. util. Iterator;
public class Demo1 {
public static void main ( String[ ] args) {
ArrayList list = new ArrayList ( ) ;
list. add ( "a" ) ;
list. add ( "a" ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "b" ) ;
list. add ( "b" ) ;
ArrayList list1 = new ArrayList ( ) ;
list1 = getSingle ( list) ;
System. out. println ( list1) ;
}
public static ArrayList getSingle ( ArrayList list) {
ArrayList list1 = new ArrayList ( ) ;
Iterator it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Object obj = it. next ( ) ;
if ( ! list1. contains ( obj) ) {
list1. add ( obj) ;
}
}
return list1;
}
}
2. package com. guada. list;
import java. util. LinkedList;
public class Demo3 {
public static void main ( String[ ] args) {
LinkedList list = new LinkedList ( ) ;
list. addFirst ( "d" ) ;
list. addFirst ( "c" ) ;
list. addFirst ( "b" ) ;
list. addFirst ( "a" ) ;
list. addLast ( "e" ) ;
System. out. println ( list) ;
System. out. println ( list. getFirst ( ) ) ;
System. out. println ( list. getLast ( ) ) ;
list. removeFirst ( ) ;
list. removeLast ( ) ;
System. out. println ( list) ;
System. out. println ( list. get ( 1 ) ) ;
}
}
3. package com. guada. list;
import java. util. LinkedList;
public class Stack {
private LinkedList list = new LinkedList ( ) ;
public void in ( Object obj) {
list. addLast ( obj) ;
}
public Object out ( ) {
return list. removeLast ( ) ;
}
public boolean isEmpty ( ) {
return list. isEmpty ( ) ;
}
}
package com. guada. list;
public class Demo4 {
public static void main ( String[ ] args) {
Stack s = new Stack ( ) ;
s. in ( "a" ) ;
s. in ( "b" ) ;
s. in ( "c" ) ;
s. in ( "d" ) ;
while ( ! s. isEmpty ( ) ) {
System. out. println ( s. out ( ) ) ;
}
System. out. println ( s) ;
}
}
4. package com. guada. list;
import java. util. ArrayList;
import java. util. Iterator;
import java. util. LinkedList;
public class Demo5_Generic {
public static void main ( String[ ] args) {
}
public static void demo1 ( ) {
LinkedList< Person> list = new LinkedList < Person> ( ) ;
list. add ( new Person ( "张三" , 23 ) ) ;
list. add ( new Person ( "李四" , 24 ) ) ;
Iterator< Person> it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Person p = it. next ( ) ;
System. out. println ( p. getName ( ) + "-----" + p. getAge ( ) ) ;
}
}
}
5. 方法泛型最好与类的泛型一致
如果不一致,需要在方法上声明该泛型
静态方法必须声明自己的泛型
```java
```java
6. package com. guada. list;
public class Demo6_Generic {
public static void main ( String[ ] args) {
}
}
interface Inter < T> {
public void show ( T t) ;
}
class Demo < T> implements Inter < T> {
@Override
public void show ( T t) {
System. out. println ( t) ;
}
}
7. package com. guada. list;
import java. util. ArrayList;
import java. util. List;
public class Demo7_Generic {
public static void main ( String[ ] args) {
List< ? > list1 = new ArrayList < String> ( ) ;
}
}
8. package com. guada. list;
import java. util. ArrayList;
public class Demo8_Foreach {
public static void main ( String[ ] args) {
int [ ] arr = { 11 , 22 , 33 , 44 , 55 } ;
for ( int i : arr) {
System. out. println ( i) ;
}
ArrayList< String> list = new ArrayList < String> ( ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "c" ) ;
list. add ( "d" ) ;
for ( String string : list) {
System. out. println ( string) ;
}
}
}
9. package com. guada. list;
import java. util. ArrayList;
import java. util. Iterator;
import java. util. List;
public class Demo9_Delect {
public static void main ( String[ ] args) {
List< String> list = new ArrayList < String> ( ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "b" ) ;
list. add ( "c" ) ;
list. add ( "d" ) ;
for ( String string : list) {
if ( "b" . equals ( string) ) {
list. remove ( string) ;
}
}
System. out. println ( list) ;
}
}
10. 静态导入:
静态导入是导入类中的静态方法
格式:
import static 包名/ 类名. 方法名;
能看懂即可,在实际的开发中不使用
11.
可变参数:
定义方法的时候不知道该定义多少个参数
格式:
修饰符 返回值类型 方法名(数据类型 变量名){ }
这里的变量其实是一个数组
如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
12. package com. guada. list;
import java. util. Arrays;
import java. util. List;
public class Demo10_ChangeableArgs {
public static void main ( String[ ] args) {
String [ ] arr = { "a" , "b" , "c" } ;
List< String> list = Arrays. asList ( arr) ;
System. out. println ( list) ;
list. add ( "d" ) ;
System. out. println ( list) ;
}
}
13. package com. guada. list;
import java. util. ArrayList;
public class Demo11_ArrayListArrayList {
public static void main ( String[ ] args) {
ArrayList< ArrayList< Person> > list = new ArrayList < ArrayList< Person> > ( ) ;
ArrayList< Person> first = new ArrayList < Person> ( ) ;
first. add ( new Person ( "张三" , 23 ) ) ;
first. add ( new Person ( "李四" , 24 ) ) ;
first. add ( new Person ( "王五" , 25 ) ) ;
ArrayList< Person> second = new ArrayList < Person> ( ) ;
first. add ( new Person ( "小明" , 26 ) ) ;
first. add ( new Person ( "小芳" , 27 ) ) ;
first. add ( new Person ( "小红" , 28 ) ) ;
list. add ( first) ;
list. add ( second) ;
System. out. println ( list) ;
}
}