.


:




:

































 

 

 

 


2 2




EL

, HashMap<K,V> Map.Entry<K,V> .

/* # 15: :

DemoSecurity.java */

package chapt10;

import java.util.*;

 

public class DemoSecurity {

public static void main(String[] args) {

CheckRight. startUsing (2041, "");

CheckRight. startUsing (2420, "");

/*

*

*

*/

CheckRight. startUsing (2437, "");

CheckRight. startUsing (2041, "");

}

}

/* # 16: : CheckRight.java */

package chapt10;

import java.util.*;

 

public class CheckRight {

private static HashMap<Integer, String> map =

new HashMap<Integer, String> ();

 

public static void startUsing(int id, String name) {

if (canUse(id)){

map.put(id, name);

System. out. println(" ");

} else {

System. out. println(" ");

}

}

public static boolean canUse(int id) {

final int MAX_NUM = 2; // 2 3

int currNum = 0;

if (!map.containsKey(id))

currNum = map.size();

return currNum < MAX_NUM;

}

}

:

,

. MAX_NUM , 2, .

EnumMap<K extends Enum<K>, V> , enum, . .

/* # 17: EnumMap: UseEnumMap.java */

package chapt10;

import java.util.EnumMap;

 

enum User {

STUDENT, TUTOR, INSTRUCTOR, DEAN

}

class UserPriority {

private int priority;

 

public UserPriority(User k) {

switch (k) {

case STUDENT:

priority = 1; break;

case TUTOR:

priority = 3; break;

case INSTRUCTOR:

priority = 7; break;

case DEAN:

priority = 10; break;

default:

priority = 0;

}

}

public int getPriority() {

return priority;

}

}

public class UseEnumMap {

public static void main(String[] args) {

EnumMap<User, UserPriority> faculty =

new EnumMap<User, UserPriority> (User. class);

for (User user: User.values()) {

faculty.put(user,

new UserPriority(user));

}

for (User user: User.values()) {

System.out.println(user.name()

+ "-> Priority:" +

((UserPriority) faculty.get(user)).getPriority());

}

}

}

:

STUDENT-> Priority:1

TUTOR-> Priority:3

INSTRUCTOR-> Priority:7

DEAN-> Priority:10

, , , , , Java , Hashtable<K,V>, Vector<E> Enumeration<E>. , .

Hashtable<K,V> Map, :

Enumeration<V> elements() ( ) ;

Enumeration<K> keys() .

/* # 18: - :

HashTableDemo.java */

package chapt10;

import java.util.*;

import java.io.*;

 

public class HashTableDemo {

public static void main(String[] args) {

Hashtable<Integer, Double> ht =

new Hashtable<Integer, Double>();

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

ht.put(i, Math.atan(i));

Enumeration<Integer> ek = ht.keys();

int key;

while (ek.hasMoreElements()) {

key = ek.nextElement();

System.out.printf("%4d ", key);

}

System. out. println("");

Enumeration<Double> ev = ht.elements();

double value;

while (ev.hasMoreElements()) {

value = ev.nextElement();

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

}

}

}

:

4 3 2 1 0

1,33 1,25 1,11 0,79 0,00

, , .

Collections

Collections , . , , , , . Collections checkedCollection(): public static <E> Collection <E> checkedCollection(Collection<E> c, Class<E> type)

, ,
ClassCastException:

/* # 19: : SafeCollection.java */

package chapt10;

import java.util.*;

 

public class SafeCollection {

public static void main(String args[]) {

Collection c = Collections.checkedCollection(

new HashSet<String>(), String. class);

c.add("Java");

c.add(7.0); //

}

}

, , : checkedList(),
checkedSortedMap(), checkedMap(), checkedSortedSet(),
checkedSet(), :

<T> boolean addAll(Collection<? super T> c, T... a) ;

<T> void copy(List<? super T> dest, List<? extends T> src) ;

boolean disjoint(Collection<?> c1, Collection<?> c2) true, ;

<T> List <T> emptyList(), <K,V> Map <K,V> emptyMap(),
<T> Set <T> emptySet()
,
;

<T> void fill(List<? super T> list, T obj) ;

int frequency(Collection<?> c, Object o) ;

<T extends Object & Comparable <? super T>> T max(Collection<? extends T> coll),

<T extends Object & Comparable <? super T>> T min(Collection<? extends T> coll)
;

<T> T max(Collection <? extends T> coll,

Comparator<? super T> comp),

<T> T min(Collection<? extends T> coll,

Comparator<? super T> comp) , Comparator ;

<T> List <T> nCopies(int n, T o) n ;

<T> boolean replaceAll(List<T> list, T oldVal, T newVal) ;

void reverse(List<?> list) ;

void rotate(List<?> list, int distance) ;

void shuffle(List<?> list) ;

<T> Set <T> singleton(T o), singletonList(T o), singletonMap(K key, V value) , , ;

<T extends Comparable<? super T>> void sort(List<T> list),

<T> void sort(List<T> list, Comparator<? super T> c) , Comparator ;

void swap(List<?> list, int i, int j) .

/* # 20: Collections: CollectionsDemo.java:

MyComparator.java */

package chapt10;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

 

public class CollectionsDemo {

public static void main(String[] args) {

MyComparator<Integer> comp =

new MyComparator<Integer>();

ArrayList<Integer> list =

new ArrayList<Integer>();

 

Collections. addAll (list, 1, 2, 3, 4, 5);

Collections. shuffle (list);

print(list);

Collections. sort (list, comp);

print(list);

Collections. reverse (list);

print(list);

Collections. rotate (list, 3);

print(list);

System. out. println("min: "

+ Collections.min(list, comp));

System. out. println("max: "

+ Collections.max(list, comp));

 

List<Integer> singl =

Collections.singletonList(71);

print(singl);

//singl.add(21);//

}

static void print(List<Integer> c) {

for (int i: c)

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

System.out.println();

}

}

package chapt10;

import java.util.Comparator;

 

public class MyComparator<T> implements Comparator<Integer> {

public int compare(Integer n, Integer m) {

return m.intValue() - n.intValue();

}

}

:

4 3 5 1 2

5 4 3 2 1

1 2 3 4 5

3 4 5 1 2

min: 5

max: 1

Arrays

java.util Arrays, , , , , :

int binarySearch( ) . ;

void fill( ) ;

void sort( ) Comparator ;

static <T> T[] copyOf(T[] original, int newLength) , null ;

static <T> T[] copyOfRange(T[] original, int from, int to) ;

<T> List<T> asList(T... a) , List<T>.

.

/* # 21: Arrays: ArraysEqualDemo.java */

package chapt10;

import java.util.*;

public class ArraysEqualDemo {

public static void main(String[] args) {

char m1[] = new char [3];

char m2[] = { 'a', 'b', 'c' }, i;

Arrays.fill(m1, 'a');

System.out.print(" m1:");

for (i = 0; i < 3; i++)

System.out.print(" " + m1[i]);

m1[1] = 'b';

m1[2] = 'c';

//m1[2]='x'; //

if (Arrays.equals(m1, m2))

System.out.print("\nm1 m2 ");

Else

System.out.print("\nm1 m2 ");

 

m1[0] = 'z';

Arrays.sort(m1);

System.out.print("\n m1:");

for (i = 0; i < 3; i++)

System.out.print(" " + m1[i]);

System.out.print(

"\n 'c' -"

+ Arrays.binarySearch(m1, 'c'));

Integer arr[] = {35, 71, 92};

//

System.out.println(Arrays.deepToString(arr));

// -

System.out.println(Arrays.deepHashCode(arr));

Integer arr2[] = {35, 71, 92};

//

System.out.println(Arrays.deepEquals(arr, arr2));

char m3[] = new char [5];

//

m3 = Arrays. copyOf (m1, 5);

System. out. print(" m3:");

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

System. out. print(" " + m3[i]);

}

}

:

m1: a a a

M1 m2

m1: b c z

'c' 1

[35, 71, 92]

True

m3: b c z □ □

10

A

1. , . .

2. , . , .

3. .

4. .

5. . .

6. . .

7. , .

8. . .

9. (, ) I(1..n) U(1..n) n- R. R .

10. : , .. , .

11. , HashMap.

12. , .

13. , , .

14. , ArrayList. , sort() Collections.

15. , '(', ')', '[', ']', '{', '}'. . .

16. . . , , . HashSet.

17. . . . , , . HashMap.

B

1. N , 1 N. , . , . ArrayList, LinkedList. ? ?

2. X. , , , X, , X.

3. , . . PriorityQueue.

4. Graph, . . .

5. :

/ ;

, (.. ).

6. , N- . , . , .

7. . , . , : ) S = A + B; ) P = A * B.

8. . C1, , . C1, .

9. ; . C1 2, 1- 2- , . C1 2 , .

10. , . . , , , , , . . .

11. , , , K1 K2. , K1 K2 . , , , 1 2 20- ( ).

12. N . , . , . HashMap.

13. . , , . PriorityQueue.

14. N . , . TreeMap.

15. . , . ,
. , , 90 , . HashSet.

16. . , . Stack.

17. " ", K, . K- .

18. N , . , .

19. N , . K .

10

10.1.

, ?

1) Set;

2) List;

3) Map;

4) Vector;

5) .

10.2.

ArrayList ?

1) ArrayList a = new ArrayList(); a.add(0);

2) ArrayList a = new ArrayList(); a[0]=0;

3) List a = new List(); a.add(0);

4) List a = new ArrayList(10); a.add(0);

10.3.

Hashtable?

1) Set;

2) Vector;

3) AbstractMap;

4) List;

5) Map.

10.4.

:

import java.util.*; class Quest4 { public static void main (String args[]) { Object ob = new HashSet();System.out.print((ob instanceof Set) + ", ");System.out.print(ob instanceof SortedSet);}}

?

1) true, false;

2) true, true;

3) false, true;

4) false, false;

5) .

10.5.

java.util?

1) SortedMap;

2) HashMap;

3) HashSet;

4) SortedSet;

5) Stack;

6) AbstractMap.

 
11

Java , , (, , .), . Java :

AWT( java.awt) , . , .

Swing ( javax.swing, javax , , ) , AWT. J (JButton, JLabel
..). JFC (Java Foundation Classes), JavaBeans, .

Swing, AWT, - . Swing , , . , Swing , .

Java . , Web- Web-. .

, Panel, , Frame, Window.

AWT Swing, , 11.1.

java.awt.Component , . , .

. 11.1. AWT Swing

Container add(), () , . Container Panel Window .





:


: 2016-04-03; !; : 1893 |


:

:

- , - .
==> ...

1747 - | 1663 -


© 2015-2024 lektsii.org - -

: 0.136 .