◆ Nested Loop
- Put loops together!
- 2중 for문/3중 for문/ .. 등을 일컫는 말
◆ Random Numbers and Simulations
- In a simulation, you repeatedly generate random numbers and use them to simulate an activity
- Random number generator
Random generator = new Random();
int n = generator.nextInt(a); // 0 < = n < a
double x = generator.nextDouble(); // 0 <= x < 1
◆ Arrays
- Array : Sequence of values of the same type
- Construct array
new double[10];
- Store in variable of type double[]:
double[] data = new double[10];
- When array is created, all values are initialized depending on array type ;
* Numbers : 0
* Boolean : false
* Object References : null
- Use [] to access an element
- Get array length as values.length (Not a method!-> don't length())
- Index values range from 0 to length -1
- Accessing a nonexistent element results in a bound error
- Limitation : Arrays have fixed length
◆ Array Lists (c로 비유하자면, 링크드리스트를 이용한 배열 느낌)
- ArrayList class manage a sequence of objects
- Can grow and shrink as needed
- ArrayList class supplies methods for many common tasks, such as inserting and removing elements
- ArrayList is a generic class :
ArrayList<T>
collects objects of type parameter T :
ArrayList<String> names = new ArrayList<String>(); // (사이즈 지정 X)
names.add("emily");
names.add("bob");
names.add("cindy");
- size method yields number of elements
* Type T can be String and any Object, only
* Array List >> add, set, get, remove
◆ Array List Elements
- add : To add an object to the end of the array list
names.add("Emily"); // names.add(element);
- get : To obtain the value an element at an index
String name = names.get(2); // names.get(index#);
- set : To set an element to a new value (Replace value)
names.set(2, "Carolyn"); // names.set(index#,element);
- remove : To remove an element at an index
names.remove(1); //names.remove(1);
**caution: after removing, Array list shrinks automatically
ex. [a][b][c]
remove(1); //remove [b]
[a][c]
◆ Wrapper Classes
* ArrayList 는 Primitive Type을 지원하지 않는다. Wrapper class를 이용하자!
Wrapper class로 primitive type의 object를 만들고 array list에 적용하면 된다!
- For each primitive type, there is a wrapper class for storing values of that type
Double d = new double (29.95);
- Wrapper objects can be used anywhere that objects are required instead of primitive type values
* Wrappers
Primitive Type | Wrapper |
byte | Byte |
boolean | Boolean |
char | Character |
double | Double |
float | Float |
int | Integer |
long | Long |
short | Short |
◆ Common Array Algorithm ... self study 추천
'Computers > Language java' 카테고리의 다른 글
week9. Interfaces and Polymorphism (0) | 2011.04.10 |
---|---|
week8. Graphic User Interface (0) | 2011.04.10 |
week5. Decision (0) | 2011.04.10 |
week4. Fundamental Data Types (0) | 2011.04.10 |
week3. Implementing Classes (Basic Java Concept part2) (0) | 2011.04.10 |