package com.jihe;
import java.util.ArrayList;
public class ArrayListDemo1 {
public static void main(String[] args) {
//创建一个ArrayList实例
ArrayList<String> fruits = new ArrayList<>();
//添加元素
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
//打印列表大小
System.out.println("List size:"+fruits.size());
//检查列表是否包含某个元素
if(fruits.contains("Banana")){
System.out.println("Banana is in the list.");
}
///获取并打印指定索引处的元素
String secondFruit = fruits.get(1);
System.out.println("Second fruit is:"+secondFruit);
//在指定位置插入一个元素
fruits.add(1,"Berry");
System.out.println("List after insertion:" + fruits);
//替换指定位置的元素
fruits.set(1,"Blackberry");
System.out.println("List after replacement: "+fruits);
//删除指定位置的元素
fruits.remove(1);
System.out.println("List after deletion: "+fruits);
//清空列表
fruits.clear();
System.out.println("List is empty now? "+fruits.isEmpty());
}
}
package com.jihe;
public class CircularArrayQueue<E> implements Queue<E>{
private int head;
private int tail;
CircularArrayQueue(int capacity){}
@Override
public void add(E element) {
}
@Override
public E remove() {
return null;
}
@Override
public int size() {
return 0;
}
private E[] elements;
}
package com.jihe;
public interface Collection <E>{
boolean add(E element);
Iterable<E> iterator();
}
package com.jihe;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionSortDemo1 {
public static void main(String[] args) {
//定义数组
Integer[] array = {50,100,30,120,10,20};
//2.对数组元素进行排序
//基于Collections工具类对数组或集合进行排序
List<Integer> list = Arrays.asList(array);//将数组转换为list集合
Collections.sort(list);
System.out.println(list);
}
}
package com.jihe;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionSortDemo02 {
public static void main(String[] args) {
Rect p1 = new Rect(10,20);
Rect p2 = new Rect(5,30);
Rect p3 = new Rect(8,30);
List list = new ArrayList<Rect>();
list.add(p1);
list.add(p2);
list.add(p3);
//调用Collections集合的sort方法对集合元素进行排序
Collections.sort(list);
System.out.println(list);
}
}
package com.jihe;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionSortDemo3 {
public static void main(String[] args) {
Point p1 = new Point(10,20);
Point p2 = new Point(5,30);
List<Point> list = new ArrayList<>();
list.add(p1);
list.add(p2);
//调用
Comparator<Point> comparator1 = new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
return o1.getX() - o2.getX();
}
};
Comparator<Point> comparator2 = new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
return o1.getY() - o2.getY();
}
};
Collections.sort(list,comparator1);
System.out.println(list);
Collections.sort(list,comparator2);
System.out.println(list);
}
}
package com.jihe;
import javafx.beans.binding.ObjectExpression;
import java.util.Comparator;
import java.util.Objects;
public class Item implements Comparator<Item> {
private String description;
private int partNumber;
public Item(String aDescription,int aPartNumber){
description = aDescription;
partNumber = aPartNumber;
}
public String getDescription()
{
return description;
}
public String toString(){
return "[description="+description+", partNumber="+partNumber+"]";
}
@Override
public int compare(Item o1, Item o2) {
return 0;
}
public boolean equals(Object otherObject)
{
if(this == otherObject)return true;
if(otherObject == null)return false;
if(getClass() != otherObject.getClass())return false;
Item other = (Item) otherObject;
return Objects.equals(description,other.description) && partNumber == other.partNumber;
}
public int hashCode(){
return Objects.hash(description,partNumber);
}
public int compare(Item other)
{
int diff = Integer.compare(partNumber,other.partNumber);
return diff != 0 ? diff : description.compareTo(other.description);
}
}
package com.jihe;
import java.util.function.Consumer;
public interface Iterator <E>{
E next();
boolean hasNext();
void remove();
default void forEachRemaining(Consumer<? super E> action) {
}
}
package com.jihe;
import sun.plugin.javascript.navig4.Link;
public class LinkedListQueue<E> implements Queue<E>{
private Link head;
private Link tail;
LinkedListQueue(){}
@Override
public void add(E element) {
}
@Override
public E remove() {
return null;
}
@Override
public int size() {
return 0;
}
}
package com.jihe;
import java.util.*;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<String> a = new LinkedList<String>();
a.add("Amy");
a.add("Carl");
a.add("Erica");
LinkedList<String> b = new LinkedList<String>();
b.add("Bob");
b.add("Doug");
b.add("Frances");
b.add("Gloria");
ListIterator<String> aIter = a.listIterator();
java.util.Iterator<String> bIter = b.iterator();
while (bIter.hasNext())
{
if(aIter.hasNext())aIter.next();
aIter.add(bIter.next());
}
System.out.println(a);
//remove every second word from b
bIter = b.iterator();
while(bIter.hasNext())
{
bIter.next();//skip one element
if(bIter.hasNext())
{
bIter.next();//skip next element
bIter.remove();//remove that
}
}
System.out.println(b);
a.removeAll(b);
System.out.println(a);
}
}
package com.jihe;
import java.util.HashMap;
public class MapTest {
public static void main(String[] args) {
// HashMap staff = new HashMap<String,Employee>();
}
}
package com.jihe;
import java.time.LocalDate;
import java.util.PriorityQueue;
public class PriorityQueueTest {
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<LocalDate>();
pq.add(LocalDate.of(1906,12,9));
pq.add(LocalDate.of(1815,12,10));
pq.add(LocalDate.of(1903,12,3));
pq.add(LocalDate.of(1910,6,22));
System.out.println("Iterating over elements...");
for(Object date : pq){
System.out.println(date);
}
System.out.println("Removing elements ...");
while(!pq.isEmpty())
System.out.println(pq.remove());
}
}
package com.jihe;
public interface Queue <E>{
void add(E element);
E remove();
int size();
}
package com.jihe;
import java.util.Comparator;
public class Rect implements Comparator<Rect> {
private Integer width;
private Integer height;
public Rect(){
}
public Rect(int width,int height){
this.width = width;
this.height = height;
}
public int compareTo(Rect o){
return this.width - o.width;
}
@Override
public int compare(Rect o1, Rect o2) {
return 0;
}
}
package com.jihe;
import java.util.HashSet;
import java.util.Scanner;
public class SetTest {
public static void main(String[] args) {
HashSet words = new HashSet<String>();
long totalTime = 0;
try(Scanner in = new Scanner(System.in))
{
while(in.hasNext())
{
String word = in.next();
long callTime = System.currentTimeMillis();
words.add(word);
callTime = System.currentTimeMillis() - callTime;
totalTime += callTime;
}
}
java.util.Iterator<String> iter = words.iterator();
for(int i = 1;i<=20&& iter.hasNext();i++)
System.out.println(iter.next());
System.out.println(". . .");
System.out.println(words.size() + " distinct words. "+totalTime+" milliseconds");
}
}
package com.jihe;
import java.util.Comparator;
import java.util.TreeSet;
//class Item{
// private String name;
// private Integer id;
// public Item(String name,Integer id){
// this.name = name;
// this.id = id;
// }
//}
public class TreeSetTest {
public static void main(String[] args) {
TreeSet parts = new TreeSet<Item>();
parts.add(new Item("Toaster",1234));
parts.add(new Item("Widget",4562));
parts.add(new Item("Modem",9912));
System.out.println(parts);
TreeSet sortByDescription = new TreeSet(Comparator.comparing(Item::getDescription));
sortByDescription.addAll(parts);
System.out.println(sortByDescription);
}
}
标签:java,System,util,add,集合,import,public
From: https://blog.csdn.net/m0_68935893/article/details/141056702