.


:




:

































 

 

 

 


2




ArrayList()

ArrayList(Collection <? extends E> c)

ArrayList(int capacity)

. List<E> , :

void add(int index, E element) element , index;

void addAll(int index, Collection<? extends E> c) , index;

E get(int index)
index;

int indexOf(Object ob) ;

E remove(int index) index;

E set(int index, E element) index, ;

List<E> subList(int fromIndex, int toIndex) .

, ArrayList<E> .

/* # 1: : DemoGeneric.java */

package by.bsu. chapt10;

import java.util.*;

public class DemoGeneric {

public static void main(String args[]) {

ArrayList<String> list = new ArrayList<String>();

// ArrayList<int> b = new ArrayList<int>(); //

list.add("Java"); /*

*/

list.add("Fortress");

String res = list.get(0); /*

*/

// list.add(new StringBuilder("C#") ); //

//

System. out. print(list);

}

}

:

[Java, Fortress]

, , list. .

, , .

/* # 2: (raw): UncheckCheck.java */

package by.bsu. chapt10;

import java.util.*;

public class UncheckCheck {

public static void main(String args[]) {

ArrayList list = new ArrayList(); //

list.add(71);

list.add(new Boolean("TruE"));

list.add("Java 1.6.0");

 

//

int i = (Integer)list.get(0);

boolean b = (Boolean)list.get(1);

String str = (String)list.get(2);

for (Object ob: list) {

System. out. println("list " + ob);

}

ArrayList<Integer> s = new ArrayList<Integer>();

s.add(71);

s.add(92);

// s.add("101");// : s

for (Integer ob: s) {

System. out. println("int " + ob);

}

}

}

:

List 71

List true

List Java 1.6.0

Int 71

Int 92

, , .

Iterator . , .

/* # 3: : DemoIterator.java */

package by.bsu. chapt10;

import java.util.*;

public class DemoIterator {

public static void main(String[] args) {

ArrayList<Double> c =

new ArrayList<Double>(7);

System. out. println(" : "

+ a.isEmpty());

for (int i = 0;i < 10; i++) {

double z = new Random().nextGaussian();

c.add(z); //

}

//

for (Double d: c) {

System. out. printf("%.2f ",d);

}

int positiveNum = 0;

int size = c.size(); //

//

Iterator<Double> it = c.iterator();

 

//

while (it.hasNext()) {

//

if (it.next() > 0) {

positiveNum++;

} else {

it.remove(); //

}

}

System. out. printf("%n : %d ",

positiveNum);

System. out. printf("%n : %d ",

size - positiveNum);

System. out. println("\n ");

for (Double d: c) {

System. out. printf("%.2f ",d);

}

}

}

:

: true

0,69 0,33 0,51 -1,24 0,07 0,46 0,56 1,26 -0,84 -0,53

: 7

: 3

0,69 0,33 0,51 0,07 0,46 0,56 1,26


ListIterator<E>, . .

/* # 4: , : DemoListMethods.java */

package by.bsu. chapt10;

import java.util.*;

public class DemoListMethods {

public static void main(String[] args) {

ArrayList<Character> a =

new ArrayList<Character>(5);

for (char c = 'a'; c < 'h'; ++c) {

a.add(c);

}

char ch = 'a';

a.add(6, ch); // 6 >=8

System. out. println(a);

ListIterator<Character> it; //

it= a.listIterator(2); //

System. out. println(" "

+ it.nextIndex());

it.add('X'); //

System. out. println(a);

//

int index = a.lastIndexOf(ch); // a.indexOf(ch);

a.set(index, 'W'); //

System. out. println(a + " ");

if (a.contains(ch)) {

a.remove(a.indexOf(ch));

}

System. out. println(a + " " + ch);

}

}

:

[a, b, c, d, e, f, a, g]

2

[a, b, X, c, d, e, f, a, g]

[a, b, X, c, d, e, f, W, g]

[b, X, c, d, e, f, W, g] a

LinkedList<E> . , , , .

LinkedList<E> void addFirst(E ob), void addLast(E ob), E getFirst(),
E getLast(), E removeFirst(), E removeLast() , , .

LinkedList<E> Queue<E>, , . Queue<E> E element(), boolean offer(E o), E peek(), E poll() , E remove() , LinkedList<E>.

Queue<E>: boolean offer(E o) , ; E element() , ; E peek() , , null , ; E poll() , null , ; E remove() .

element() remove() peek() poll() , , .

/* # 5: : DemoLinkedList.java */

package by.bsu. chapt10;

import java.util.*;

public class DemoLinkedList {

public static void main(String[] args){

LinkedList<Number> a = new LinkedList<Number>();

for (int i = 10; i <= 15; i++) {

a.add(i);

}

for (int i = 16; i <= 20; i++) {

a.add(new Float(i));

}

ListIterator<Number> list = a.listIterator(10);

System. out. println("\n"+ list.nextIndex()

+ "- ");

list.next(); // !

System. out. println(list.nextIndex()

+ "- ");

list.remove(); //

while (list.hasPrevious()) {

System. out. print(list.previous()+" "); /*

*/

}

//

a.removeFirst();

a.offer(71); //

a.poll(); //

a.remove(); //

a.remove(1); //

System. out. println("\n" + a);

 

Queue<Number> q = a; //

for (Number i: q) { //

System. out. print(i + " ");

}

System. out. println(":size= " + q.size());

 

//

for (int i = 0; i < 5; i++) {

Number res = q.poll();

}

System. out. print("size= " + q.size());

}

}

:

19.0 18.0 17.0 16.0 15 14 13 12 11 10

[13, 15, 16.0, 17.0, 18.0, 19.0, 71]

13 15 16.0 17.0 18.0 19.0 71:size= 7

size= 2

Comparator<T> , . int compare(T ob1,
T ob2)
, , . public static <T> void sort(List<T> list, Comparator<? super T> c) Collections, , -comparator, .

/* # 6: : UniqSortMark.java */

package by.bsu. chapt10;

import java.util.Comparator;

public class Student implements Comparator<Student> {

private int idStudent;

private float meanMark;

 

public Student(float m, int id) {

meanMark = m;

idStudent = id;

}

public Student() {

}

public float getMark() {

return meanMark;

}

public int getIdStudent() {

return idStudent;

}

//

public int compare(Student one, Student two) {

Return

(int)(Math. ceil (two.getMark() - one.getMark()));

}

public boolean equals(Object ob){ /**/ }

}

package by.bsu. chapt10;

import java.util.*;

public class UniqSortMark {

public static void main(String[] args) {

ArrayList<Student> p = new ArrayList<Student>();

p.add(new Student(3.9f, 52201));

p.add(new Student(3.65f, 52214));

p.add(new Student(3.71f, 52251));

p.add(new Student(3.02f, 52277));

p.add(new Student(3.81f, 52292));

p.add(new Student(9.55f, 52271));

//

try {

Collections. sort (p, Student. class. newInstance ());

} catch (InstantiationException e1) {

//

e1.printStackTrace();

} catch (IllegalAccessException e2) {

e2.printStackTrace();

}

for (Student ob: p) {

System. out. printf("%.2f ", ob.getMark());

}

}

}

:

9,55 3,90 3,81 3,71 3,65 3,02

boolean equals(Object obj) Comparator<T>, , true compare() 0.

Student Student, StudentEnum:

public enum StudentEnum {

IDSTUDENT, MEANMARK

}

:

public class Student{ // Comparator

private int idStudent;

private float meanMark;

/* sortEnum

. setSortEnum() */

public static StudentEnum sortEnum =

StudentEnum. MEANMARK;

//...

}

, Comparator.

/* # 7: : StudentId.java */

package by.bsu.chapt10;

import java.util.Comparator;

public class StudentComparator

implements Comparator<Student> {

public int compare(Student one, Student two) {

 

switch (Student. sortEnum) {

case IDSTUDENT:

Return

(one.getIdStudent() - two.getIdStudent());

case MEANMARK:

Return

(int)(Math. ceil (two.getMark() - one.getMark()));

default: throw

new EnumConstantNotPresentException(

StudentEnum. class, Student. sortEnum. name());

}

}

public boolean equals(Object ob){ /**/ }

}

id sortEnum sort() StudentComparator:

Collections. sort (p, new StudentComparator());

Comparator equals() , compare() 0, equals() true. , equals() contains(Object o), java.util.Collection, , .

Deque

Deque , , . , . . , - (null false ). Deque, . .

Deque. addFirst(), addLast() . add() Queue addLast() Deque.

/* # 8: Deque: DequeRunner.java */

package by.bsu.chapt10;

import java.util.*;

public class DequeRunner {

public static void printDeque(Deque<?> d){

for (Object de: d) {

System. out. println(de + "; ");

}

}

public static void main(String[] args) {

Deque<String> deque = new ArrayDeque<String>();

deque.add(new String("5"));

deque.addFirst("A");

//deque.addLast(new Integer(5));//

System. out. println(deque.peek());

System. out. println("Before:");

printDeque (deque);

deque.pollFirst();

System. out. println(deque.remove(5));

System. out. println("After:");

printDeque (deque);

}

}

:

A

Before:

A;

5;

False

After:

5;

Set<E> , . SortedSet<E> Set<E> , , . NavigableSet .

. 2.

HashSet<E>
AbstractSet<E> Set<E>, - . (-) , . . -, - .

:

HashSet()

HashSet(Collection <? extends E> c)

HashSet(int capacity)

HashSet(int capacity, float loadFactor), capacity -.

/* # 9: : DemoHashSet.java */

package by.bsu.chapt10;

import java.io.*;

import java.util.*;

public class DemoHashSet {

public static void main(String[] args) {

HashSet<String> words =

new HashSet<String>(100);

long callTime = System. nanoTime ();

Scanner scan;

try {

scan = new Scanner(

new File("c://pushkin.txt"));

scan.useDelimiter("[^--]+");

while (scan.hasNext()) {

String word = scan.next();

words.add(word.toLowerCase());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

Iterator<String> it = words.iterator();

while (it.hasNext()) {

System. out. println(it.next());

}

TreeSet<String> ts =

new TreeSet<String>(words);

System. out. println(ts);

long totalTime = System. nanoTime () - callTime;

System. out. println(" : "

+ words.size() + ", "

+ totalTime + " ");

}

}

TreeSet<E> . . , Comparator Comparable. , -, , .

:

TreeSet()

TreeSet(Collection <? extends E> c)

TreeSet(Comparator <? super E> c)

TreeSet(SortedSet <E> s)

TreeSet<E> ( ) E first() E last(). SortedSet<E> subSet(E from, E to),

SortedSet<E> tailSet(E from)

SortedSet<E> headSet(E to)

.
Comparator <? super E> comparator()
Comparator, null, .

/* # 10: : DemoTreeSet.java */

package by.bsu.chapt10;

import java.util.*;

public class DemoTreeSet {

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<String>();

boolean b;

for (int i = 0; i < 6; i++) {

list.add((int) (Math. random () * 71) + "Y ");

}

System. out. println(list + "");

TreeSet<String> set = new TreeSet<String>(list);

System. out. println(set + "");

b = set.add("5 Element"); // (b=true)

b = set.add("5 Element"); // (b=false)

//

System. out. println(set + "add");

System. out. println(set.comparator()); //null!!!

//

System. out. println(set.last() + " "

+ set.first());

}

}

:

[44Y, 56Y, 49Y, 26Y, 49Y, 2Y ]

[2Y, 26Y, 44Y, 49Y, 56Y ]

[2Y, 26Y, 44Y, 49Y, 5 Element, 56Y ]add

Null

Y 2Y

. . . , , , comparator() null.

String StringBuilder
StringBuffer, TreeSet . StringBuilder Comparable<T> :

/* # 11: , TreeSet: Message.java */

package by.bsu.chapt10;

import java.util.*;

public class Message implements Comparable<Message> {

private StringBuilder str;

private int idSender;

 

public Message(StringBuilder s, int id) {

this. str = s;

idSender = id;

}

public String getStr() {

return str.toString();

}

public int getIdSender() {

return idSender;

}

public int compareTo(Message a0) {

return (idSender - a0.getIdSender());

}

public boolean equals(Object ob){ /**/ }

}

.

EnumSet<E extends Enum<E>> AbstractSet. enum. enum, . , long. . , .

. EnumSet<E> noneOf(Class<E> elemType) c ,
allOf(Class<E> elementType) , . of(E first, E... rest) , .
complementOf(EnumSet<E> s) , , . range(E from, E to) , , . null NullPointerException.

/* # 12: c enum-: UseEnumSet.java */

package by.bsu.chapt10;

import java.util.EnumSet;

 

enum Faculty{ FFSM, MMF, FPMI, RFE, GEO }

 

public class UseEnumSet {

public static void main(String[] args) {

/* set1 enum ,

*/

EnumSet <Faculty> set1 =

EnumSet. range (Faculty. MMF, Faculty. RFE);

/* set2 ,
set1*/

EnumSet <Faculty> set2 =

EnumSet. complementOf (set1);

System. out. println(set1);

System. out. println(set2);

}

}

:

[MMF, FPMI, RFE]

[FFSM, GEO]

NavigableSet

NavigableSet<E>, Java SE 6, SortedSet . TreeSet<E> .

NavigableSet<E>:

.

Iterator iterator() ;

Iterator descendingIterator() ;

NavigableSet descendingSet() ;

E lower(E e) ;

E floor(E e) ;

E higher(E e) ;

E ceiling(E e) ;

E pollFirst() ;

E pollLast() ;

SortedSet<E> subSet(E fromElement, E toElement) , fromElement toElement, ;

NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) ;


, , , . fromElement toElement , fromInclusive toInclusive , . headSet , tailSet - . , .





:


: 2017-02-24; !; : 399 |


:

:

.
==> ...

1768 - | 1619 -


© 2015-2024 lektsii.org - -

: 0.233 .