:
Java.
:
.
Java . , , . Java . , , , , , , .
java.util, .
Java , . -, , . :
Collection:
Queue: Collection
Deque: Queue
List: Collection
Set: Collection
SortedSet: Set
NavigableSet: SortedSet ,
Map: , . Collection
:
AbstractCollection: , Collection
AbstractList: AbstractCollection List,
AbstractSet: AbstractCollection Set
AbstractQueue: AbstractCollection Queue,
AbstractMap: AbstractCollection Map, "-"
AbstractSequentialList: AbstractList List.
Java - , , , , :
ArrayList:
LinkedList:
|
|
ArrayDeque: , ,
HashSet: -, - -
TreeSet:
LinkedHashSet: -
PriorityQueue:
HashMap: ,
TreeMap
ArrayList.
ArrayList , AbstractList List. , ArrayList , , , .
ArrayList :
ArrayList():
ArrayList(Collection<? extendsE>col): , col.
ArrayList (intcapacity): , capacity
ArrayList , . - . ArrayList , .
List, ArrayList:
voidadd (intindex, Eobj): index obj
booleanaddAll (intindex, Collection <? extendsE > col): index col. , true, false
Eget (intindex): index
intindexOf (Objectobj): obj . , -1
intlastIndexOf (Objectobj): obj . , -1
Eremove (intindex): index,
Eset (intindex, Eobj): obj , index
void sort(Comparator<? super E> comp): comp
List < E > subList (intstart, intend): , start end
ArrayList :
import java.util.ArrayList; public class CollectionApp { public static void main(String[] args) { ArrayList<String> states = new ArrayList<String>(); // states.add(""); states.add(""); states.add(""); states.add(""); states.add(1, ""); // 1 System.out.println(states.get(1));// 2- states.set(1, ""); // 2- System.out.printf(" %d \n", states.size()); for(String state: states){ System.out.println(state); } if(states.contains("")){ System.out.println(""); } // states.remove(""); states.remove(0); Object[] countries = states.toArray(); for(Object country: countries){ System.out.println(country); } } } |
ArrayList String, . ArrayList Collection<E>, .
|
|
add. : states.add(""). , , ( 1, ): states.add(1, "")
size() .
contains. remove. , , states.remove("");, states.remove(0); - .
get(): Stringstate = states.get(1);, set: states.set(1, "");
toArray() .
ArrayList Iterable, for-each: for(Stringstate: states).
ArrayList , , ArrayList . 10 . , , . . , , , 25, , : ArrayList<String>states = newArrayList<String>(25);, ensureCapacity: states.ensureCapacity(25);
LinkedList.
LinkedList<E> . AbstractSequentialList List, Dequeue Queue.
LinkedList :
LinkedList():
LinkedList(Collection<? extends E>col): , col
LinkedList , :
addFirst() / offerFirst():
addLast() / offerLast():
removeFirst() / pollFirst():
removeLast() / pollLast():
getFirst() / peekFirst():
getLast() / peekLast():
:
packagecollectionapp; import java.util.LinkedList; public class CollectionApp { public static void main(String[] args) { LinkedList<String> states = new LinkedList<String>(); // states.add(""); states.add(""); states.addLast(""); // states.addFirst(""); // states.add(1, ""); // 1 System.out.printf(" %d \n", states.size()); System.out.println(states.get(1)); states.set(1, ""); for(String state: states){ System.out.println(state); } // if(states.contains("")){ System.out.println(" "); } states.remove(""); states.removeFirst(); // states.removeLast(); // LinkedList<Person> people = new LinkedList<Person>(); people.add(new Person("Mike")); people.addFirst(new Person("Tom")); people.addLast(new Person("Nick")); people.remove(1); // for(Person p: people){ System.out.println(p.getName()); } Person first = people.getFirst(); System.out.println(first.getName()); // } } class Person{ private String name; public Person(String value){ name=value; } String getName(){return name;} } |
: Person. addFirst/removeLast .., , Collection: add(), remove, contains, size . . , : states.addFirst("");, : states.add(0, "");
|
|
1. ArrayList.
2. LinkedList.
3. , ArrayList.
8
:
Java. .
:
. FileWriter
FileWriter Writer. .
FileWriter, :
FileWriter(File file) FileWriter(File file, boolean append) FileWriter(FileDescriptorfd) FileWriter(String fileName) FileWriter(String fileName, boolean append) |
, , File, . append , ( true), .
- :
import java.io.*; public class FilesApp { public static void main(String[] args) { try(FileWriter writer = new FileWriter("C:\\SomeDir\\notes3.txt", false)) { // Stringtext = " , "; writer.write(text); // writer.append('\n'); writer.append('E'); writer.flush(); } catch(IOException ex){ System.out.println(ex.getMessage()); } } } |
append false - . , Writer .
|
|
. FileReader
FileReader Reader .
FileReader :
FileReader(String fileName) FileReader(File file) FileReader(FileDescriptorfd) |
, Reader, :
import java.io.*; public class FilesApp { public static void main(String[] args) { try(FileReader reader = new FileReader("C:\\SomeDir\\notes3.txt")) { // int c; while((c=reader.read())!=-1){ System.out.print((char)c); } } catch(IOException ex){ System.out.println(ex.getMessage()); } } } |
1.
2.
3. ,
4. ,
1 , Java
2 . , . . - 2016 240 . ISBN: 5906818256
3 .. .: . , 2003. -.342 .
4 .. QNX Neutrino: . 2- - .: -, 2004. 192 .
1. .. - Java, , , 2016
2. Java: . . [ ]: - . . .: , 2015. 24 .
3. , .. Java: : . . [ ]: / .. , .. . . . .: , 2012. 67 .
4. , .. . 1. [ ] / .. , .. . . . .: . .. , 2007. 59 .