1、基本方法
package enumeration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class C {
public static void main(String[] args) {
List list = new ArrayList();
//创建一个ArrayList,用接口来接收。牢记那张图。
list.add("jack");//只要是Object的子类都可以放。
list.add(10);//这里自动装箱了成为Integer。
list.add(true);
System.out.println("list =" + list );
// list.remove(0);//删除第一个元素
// list.remove("true");
boolean k = list.contains("k");//看集合是否包含
System.out.println(k);
int num = list.size();//看集合的个数
list.isEmpty();//看是否为空。
// list.clear();将其清空
// list.addAll();添加多个元素
// list.addAll(Collection o);只要是实现了Collection接口的子类都可以传进去。
ArrayList list2 = new ArrayList();
list2.add("woaini");
list2.add("yidingyaozaiyiqi");
list.addAll(list2);
System.out.println(list);
//containsAll:查找多个元素是否存在
System.out.println(list.containsAll(list2));
list.removeAll(list2);
}
}
2、

package Gather;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class gather {
public static void main(String[] args) {
Collection a = new ArrayList();
a.add(new Book("向上生长","paoge",12.5));
a.add(new Book("乌合之众","siji",11.5));
a.add(new Book("通信原理","fan",52.5));
System.out.println(a);
//(1)先得到a对应的迭代器
Iterator iterator = a.iterator();
//(2)使用while循环遍历
while(iterator.hasNext()){//判断是否 还有数据
Object obj = iterator.next();//作用(1)将指针下移(2)将下移之前的元素返回(3)类型是Object
System.out.println(obj);
}
//还有一个快捷键哦
// while (iterator.hasNext()) {
// Object obj = iterator.next();
//
// } itit快捷键
//(3)档推出while循环后,此时iterator迭代器指向最后的元素
// iterator.next();再次取值的话,就会出现异常啊,因为此时已经到达了最下面了
//(4)希望再次遍历的话,需要重置迭代器:
iterator = a.iterator();//需要重置迭代器,再一次指向最前方。
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
增强for
package Gather;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class gather {
public static void main(String[] args) {
Collection a = new ArrayList();
a.add(new Book("向上生长","paoge",12.5));
a.add(new Book("乌合之众","siji",11.5));
a.add(new Book("通信原理","fan",52.5));
//增强for循环:也可以用在数组上,本质上也是一个iterator,可以理解成简化版本的迭代器。
//快捷键I。
for(Object book : a){
System.out.println(book);
}
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
list的相关方法
package Gather;
import java.util.ArrayList;
import java.util.List;
public class list {
public static void main(String[] args) {
//(1)List集合类中的元素有序(添加顺序和取出的顺序一致,且可以重复)
List list = new ArrayList();
list.add("jack");
list.add("mary");
list.add("a");
list.add("j");
list.add(1,"j");//没有加入索引的时候默认加在最后面
list.remove(4);//删除指定下表的元素
list.set(0,"xiaozhu");//将下表索引为0的元素替换成xiaozhu
// list subList(int fromList ,int tolist) 返回指定位置的值到一个行的子集合,左闭右开。
List list1 = list.subList(0, 2);
System.out.println(list1);
System.out.println(list.lastIndexOf("j"));
System.out.println("list " + list);
//(2)List集合中的每个元素都具有其对应的索引
}
}
三种方法遍历集合:
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println("list" + next);
}
for (Object o :list) {
System.out.println(o);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
对集合里面的某项数据(对象中的某个数据)进行排序
package Gather;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Price {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Book("通信原理","樊",125.2));
list.add(new Book("信号","樊",25.2));
list.add(new Book("电磁场原理","樊",12));
//list.get(i);得到第j个集合
for (Object o :list) {
System.out.println(o);
}
//排序
bubble(list);
System.out.println("排序后的值");
for (Object o :list) {
System.out.println(o);
}
}
public static void bubble(List list){
for (int i = 0; i < list.size() - 1; i++) {
for (int j = 0; j < list.size() - 1 - i; j++) {
Book book1 = (Book)list.get(j);
Book book2 = (Book)list.get(j + 1);
if (book1.getPrice() > book2.getPrice()){
list.set(j,book2);
list.set(j+1,book1);
//集合就不用进行交换了,直接用set方法
//因为他是引用类型,所以直接改变原来的值
}
}
}
}
}
class Book{
private String name ;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
1548

被折叠的 条评论
为什么被折叠?



