Appearance
全局配置
Appearance
需求说明:按照以下实现的步骤,使用ArrayList获取和打印新闻标题,显示效果如下图所示: (1)创建多个各类新闻标题对象,包含ID、名称和创建者三个属性; (2)创建存储各类新闻标题的集合对象; (3)按照顺序依次添加各类新闻标题,使用add()方法; (4)获取新闻标题的总数,使用size()方法; (5)根据位置获取相应新闻标题、逐条打印每条新闻标题的名称,使用for循环遍历。
public class NewsTitle {
private int id;
private String name;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 NewsTitle(int id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
@Override
public String toString() {
return "NewsTitle [id=" + id + ", name=" + name + ", author=" + author + "]";
}
}
import java.util.ArrayList;
import com.southwind.entity.NewsTitle;
import com.southwind.entity.Student;
public class Test {
public static void main(String[] args) {
//创建新闻标题对象
NewsTitle nt1 = new NewsTitle(1, "体育新闻", "张三");
NewsTitle nt2 = new NewsTitle(2, "财经新闻", "李四");
NewsTitle nt3 = new NewsTitle(3, "娱乐新闻", "王五");
//创建集合对象,保存新闻标题对象
ArrayList<NewsTitle> list = new ArrayList<NewsTitle>();
list.add(nt1);
list.add(nt2);
list.add(nt3);
System.out.println("新闻标题的数目为:"+list.size());
for(int i = 0; i < list.size(); i++) {
NewsTitle nt = list.get(i);
System.out.println(nt.getId()+":"+nt.getName());
}
}
}
需求说明:按照以下实现步骤,使用ArrayList存储狗狗信息,使用ArrayList的方法对狗狗信息进行删除、读取和判断,显示效果如下图所示: (1)存储多条狗信息,获取狗总数,逐条打印出各条狗信息; (2)删除指定位置的狗,使用remove()方法; (3)判断集合中是否包含指定狗,使用contains()方法;
public class Dog implements Comparable<Dog>{
private String name;
private String strain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public Dog(String name, String strain) {
super();
this.name = name;
this.strain = strain;
}
@Override
public String toString() {
return "Dog [name=" + name + ", strain=" + strain + "]";
}
@Override
public int compareTo(Dog o) {
// TODO Auto-generated method stub
return -1;
}
}
import java.util.ArrayList;
import com.southwind.entity.Dog;
public class Test {
public static void main(String[] args) {
ArrayList<Dog> list = new ArrayList<Dog>();
list.add(new Dog("欧欧", "雪纳瑞"));
list.add(new Dog("亚亚", "拉布拉多"));
list.add(new Dog("菲菲", "拉布拉多"));
list.add(new Dog("美美", "雪纳瑞"));
System.out.println("共计有"+list.size()+"条狗狗");
System.out.println("分别是:");
for (Dog dog : list) {
System.out.println(dog.getName()+"\t"+dog.getStrain());
}
list.remove(0);
list.remove(list.get(2));
System.out.println("删除之后还有"+list.size()+"条狗狗");
System.out.println("分别是:");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
Dog dog = (Dog) iterator.next();
System.out.println(dog.getName()+"\t"+dog.getStrain());
}
Dog dog = list.get(0);
boolean flag = list.contains(dog);
if(flag) {
System.out.println("集合中存在亚亚");
}else {
System.out.println("集合中没有亚亚");
}
}
}
需求说明:在1的基础上,换用LinkedList存储新闻数据,并且使用LinkedList的getFirst()和getLast()方法获取第一条和最后一条数据,以及removeFirst()和removeLast()方法删除第一条和最后一条数据。
public class NewsTitle {
private int id;
private String name;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 NewsTitle(int id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
@Override
public String toString() {
return "NewsTitle [id=" + id + ", name=" + name + ", author=" + author + "]";
}
}
import java.util.LinkedList;
import com.southwind.entity.NewsTitle;
public class Test {
public static void main(String[] args) {
LinkedList<NewsTitle> list = new LinkedList<NewsTitle>();
NewsTitle nt1 = new NewsTitle(1, "体育新闻", "张三");
NewsTitle nt2 = new NewsTitle(2, "财经新闻", "李四");
NewsTitle nt3 = new NewsTitle(3, "娱乐新闻", "王五");
list.push(nt1);
list.push(nt2);
list.push(nt3);
System.out.println("头条的新闻标题为:"+list.getFirst().getName()); // 娱乐新闻
System.out.println("排在最后的新闻标题为:"+list.getLast().getName()); // 体育新闻
NewsTitle ntf = list.removeFirst();
NewsTitle ntl = list.removeLast();
System.out.println("删除的头条新闻标题为:"+ntf.getName()); // 娱乐新闻
System.out.println("删除的末条新闻标题为:"+ntl.getName()); // 体育新闻
}
}
需求说明:按照3的实现方式和所用到的LinkedList的方法,实现狗狗信息的更新并输出,输出效果如图所示。
package com.southwind.entity;
public class Dog implements Comparable<Dog>{
private String name;
private String strain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public Dog(String name, String strain) {
super();
this.name = name;
this.strain = strain;
}
@Override
public String toString() {
return "Dog [name=" + name + ", strain=" + strain + "]";
}
@Override
public int compareTo(Dog o) {
// TODO Auto-generated method stub
return -1;
}
}
import java.util.LinkedList;
import com.southwind.entity.Dog;
public class Test {
public static void main(String[] args) {
LinkedList<Dog> list = new LinkedList<Dog>();
list.push(new Dog("欧欧", "雪纳瑞"));
list.push(new Dog("亚亚", "拉布拉多"));
list.push(new Dog("菲菲", "拉布拉多"));
list.push(new Dog("美美", "雪纳瑞"));
System.out.println("第一条狗狗的昵称是:"+list.getFirst().getName());
System.out.println("最后一条狗狗的昵称是:"+list.getLast().getName());
list.removeFirst();
list.removeLast();
System.out.println("删除部分狗狗之后还有"+list.size()+"条狗狗");
System.out.println("分别是:");
for (Dog dog : list) {
System.out.println(dog.getName()+"\t"+dog.getStrain());
}
}
}
需求说明:按照以下实现的步骤,使用Iterator和增强型for循环遍历Set: (1)创建多个各类新闻标题对象,包含ID、名称和创建者三个属性; (2)创建存储各类新闻标题的集合对象; (3)按照顺序依次添加各类新闻标题; (4)获取新闻标题总数; (5)使用iterator()获取Iterator对象; (6)使用Iterator遍历集合,使用hasNext()方法作为循环条件,判断是否存在另一个可访问元素; (7)使用增强型for遍历集合;
package com.southwind.entity;
public class NewsTitle {
private int id;
private String name;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 NewsTitle(int id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
@Override
public String toString() {
return "NewsTitle [id=" + id + ", name=" + name + ", author=" + author + "]";
}
}
import java.util.HashSet;
import com.southwind.entity.NewsTitle;
public class Test {
public static void main(String[] args) {
HashSet<NewsTitle> set = new HashSet<NewsTitle>();
NewsTitle nt1 = new NewsTitle(1, "体育新闻", "张三");
NewsTitle nt2 = new NewsTitle(2, "财经新闻", "李四");
NewsTitle nt3 = new NewsTitle(3, "娱乐新闻", "王五");
set.add(nt1);
set.add(nt2);
set.add(nt3);
System.out.println("新闻标题数目为:"+set.size());
Iterator iteartor = set.iterator();
while(iteartor.hasNext()) {
NewsTitle nt = (NewsTitle) iteartor.next();
System.out.println(nt.getName());
}
System.out.println("---------------------");
for (NewsTitle newsTitle : set) {
System.out.println(newsTitle.getName());
}
}
}
需求说明:使用HashSet存储多条狗狗信息,获取狗狗总数,使用Iterator接口逐条打印出各条狗狗信息。
package com.southwind.entity;
public class Dog implements Comparable<Dog>{
private String name;
private String strain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public Dog(String name, String strain) {
super();
this.name = name;
this.strain = strain;
}
@Override
public String toString() {
return "Dog [name=" + name + ", strain=" + strain + "]";
}
@Override
public int compareTo(Dog o) {
// TODO Auto-generated method stub
return -1;
}
}
public class Test {
public static void main(String[] args) {
LinkedHashSet<Dog> set = new LinkedHashSet<Dog>();
set.add(new Dog("欧欧", "雪纳瑞"));
set.add(new Dog("亚亚", "拉布拉多"));
set.add(new Dog("菲菲", "拉布拉多"));
set.add(new Dog("美美", "雪纳瑞"));
System.out.println("共有"+set.size()+"条狗狗");
System.out.println("分别是:");
for (Dog dog : set) {
System.out.println(dog);
}
}
}
public class Test {
public static void main(String[] args) {
// 需实现comparable接口
TreeSet<Dog> set = new TreeSet<Dog>();
set.add(new Dog("欧欧", "雪纳瑞"));
set.add(new Dog("亚亚", "拉布拉多"));
set.add(new Dog("菲菲", "拉布拉多"));
set.add(new Dog("美美", "雪纳瑞"));
System.out.println("共有"+set.size()+"条狗狗");
System.out.println("分别是:");
for (Dog dog : set) {
System.out.println(dog);
}
}
}
需求说明:按照以下实现的步骤,使用HashMap建立国家英文简称和中文全名称间的键值映射,并通过key对value进行操作,输出效果如下图所示: (1)使用HashMap存储多组国家英文简称和中文全名称的键值对; (2)显示“CN”对应国家的中文全称: (3)显示集合中元素个数; (4)两次判断Map中是否存在“FR"键; (5)分别显示键集、值集和键值对集; (6)清空HashMap并判断;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<String,String>();
map.put("CN", "中国");
map.put("US", "美国");
map.put("RU", "俄罗斯");
map.put("FR", "法国");
System.out.println("CN对应的国家是:"+map.get("CN"));
System.out.println("Map中共有"+map.size()+"条数据");
System.out.println("Map中包含FR的key吗?"+map.containsKey("FR"));
map.remove("FR");
System.out.println("Map中包含FR的key吗?"+map.containsKey("FR"));
Set<String> keys = map.keySet();
Collection<String> values = map.values();
System.out.println(keys);
System.out.println(values);
System.out.println(map);
map.clear();
System.out.println(map.size());
}
}
需求说明:按照以下步骤,使用Iterator和增强型for循环遍历map集合。 (1)创建多个狗狗对象。 (2)创建Map集合对象,并把多个狗狗对象放入其中; (3)通过Iterator依次输出集合中所有狗狗的信息。 (4)使用增强型for循环输出集合中所有狗狗信息。
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import com.southwind.entity.Dog;
public class Test {
public static void main(String[] args) {
Hashtable<String,Dog> table = new Hashtable<String,Dog>();
table.put("欧欧", new Dog("欧欧", "雪纳瑞"));
table.put("亚亚", new Dog("亚亚", "拉布拉多"));
table.put("菲菲", new Dog("菲菲", "拉布拉多"));
table.put("美美", new Dog("美美", "雪纳瑞"));
Set<Entry<String,Dog>> entry = table.entrySet();
Iterator<Entry<String,Dog>> entryIter = entry.iterator();
while(entryIter.hasNext()) {
Entry<String,Dog> item = entryIter.next();
System.out.println(item.getKey()+"---"+item.getValue());
}
for (Entry<String, Dog> entry2 : entry) {
System.out.println(entry2.getKey()+"---"+entry2.getValue());
}
System.out.println("---------------");
Iterator<String> keyIter = table.keySet().iterator();
while(keyIter.hasNext()) {
String key = keyIter.next();
System.out.println(key+"---"+table.get(key));
}
for (String key : table.keySet()) {
System.out.println(key+"---"+table.get(key));
}
System.out.println("----------------");
Collection<Dog> collection = table.values();
Iterator<Dog> citer = collection.iterator();
while(citer.hasNext()) {
Dog dog = citer.next();
System.out.println(dog);
}
for (Dog dog : collection) {
System.out.println(dog);
}
}
}
public class Test {
public static void main(String[] args) {
TreeMap<Dog,String> set = new TreeMap<Dog,String>();
Dog dog = new Dog("欧欧", "雪纳瑞");
set.put(dog,"C");
System.out.println(dog.hashCode());
set.put(new Dog("亚亚", "拉布拉多"),"B");
set.put(new Dog("菲菲", "拉布拉多"),"A");
set.put(new Dog("美美", "雪纳瑞"),"D");
Set<Entry<Dog,String>> entry = set.entrySet();
for(Entry<Dog,String> item : entry) {
System.out.println(item.getKey().getName());
if(item.getKey().getName().equals("欧欧")) {
System.out.println(item.getKey().hashCode());
System.out.println(dog.equals(item.getKey()));
}
}
System.out.println("-------------");
System.out.println(set);
Set<Dog> keys = set.keySet();
for (Dog dog : keys) {
System.out.println(dog+"---"+set.get(dog));
}
}
}
public class Test {
public static void main(String[] args) {
HashMap<String,Student> map = new HashMap<String,Student>();
map.put("Jack", new Student("李明","男"));
System.out.println("Jack对应的学员姓名是"+map.get("Jack").getName()+",性别是"+map.get("Jack").getGender());
}
}
需求说明: 使用Collections的sort()方法升序打印输出集合中的所有元素; 使用Collections的max()方法获得集合中的最大元素; 使用Collections的min()方法获得集合中的最小元素; 使用Collections的binarySearch()方法获取元素的索引; 使用Collections的reverse()方法降序打印输出集合中的所有元素;
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
Collections.addAll(list, "and","test","collection","to","how","can","is","learn","this","we");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
Collections.reverse(list);
//binarySearch要求集合必须按照升序进行排列
System.out.println(Collections.binarySearch(list, "to"));
}
}