[IT/Programming]
For loop examples in JAVA and Javascript
kipid
2024. 4. 1. 18:46
# For loop examples in JAVA and Javascript
## PH
- 2023-09-10 : First posting.
## TOC
## JAVA Examples
```[.linenums.lang-java]
For (int i=0;i<arr.length;i++) {
// do something
arr[i], arr[i+1]
}
char[] charArr="0123456789abcdef".toCharArray();
for (char c: charArr) {
// do something
System.out.println(c);
}
Set<String> catsSet=new HashSet<>();
catsSet.add("[Music/Break]--K-pop");
For (String cat: catsSet) {
// do something
System.out.println(cat);
}
Collection<String> collection=new ArrayList<String>();
collection.add("zero");
collection.add("one");
collection.add("two");
Iterator<string> iterator=collection.iterator();
// while loop
while (iterator.hasNext()) {
// do something
System.out.println("value= "+iterator.next());
}
collection.forEach(System.out::println);
// Declaring the LinkedList
LinkedList<String> gfg=new LinkedList<String>();
// Appending new elements at
// the end of the list
gfg.add("Abhishek Rout");
gfg.add("Vaibhav Kamble");
gfg.add("Anupam Kumar");
// for loop for iterating
// conditionally through collection
System.out.println("Using For loop");
for (Iterator<String> name=gfg.iterator();name.hasNext();)
System.out.println("Name : "+name.next());
// while loop for iterating
// conditionally through collection
System.out.println("\nUsing While Loop");
Iterator<String> name=gfg.iterator();
while (name.hasNext())
System.out.println("Name : "+name.next());
List<String> strList=new ArrayList<String>();
strList.add("list item 1");
strList.add("list item 2");
strList.add("list item 3");
for (int i=0;i<strList.size();i++) {
// do something
strList.get(i)
}
```/
### Map (HashMap)
출처: , .
```[.lang-java]
Map<String, String> map=new HashMap<String, String>();
map.put("키1", "값1");
map.put("키2", "값2");
map.put("키3", "값3");
map.put("키4", "값4");
map.put("키5", "값5");
map.put("키6", "값6");
// 방법1
Iterator<String> keys=map.keySet().iterator();
while (keys.hasNext()) {
String key=keys.next();
System.out.println(String.format("키:%s, 값:%s", key, map.get(key)));
}
// 방법2
for (Map.Entry<String, String> elem: map.entrySet()) {
System.out.println(String.format("키:%s, 값:%s", elem.getKey(), elem.getValue()));
}
// 방법3
for (String key: map.keySet()) {
System.out.println(String.format("키:%s, 값:%s", key, map.get(key)));
}
// 방법4
map.forEach((key, value) -> {
System.out.println(key+":"+value);
});
// 방법5
map.entrySet().stream().forEach(e -> {
System.out.println(e.getKey()+":"+e.getValue());
});
// 방법6
map.entrySet().stream().parallel().forEach(e -> {
// parallel 은 뭐가 다른거더라??? =ㅇ=;;;
// e.getKey() + e.getValue()
});
```/
## Javascript Examples
```[.linenums.lang-js]
for (let i=0;i<arr.length;i++) {
// do something
arr[i], arr[i+1]
}
for (let key in someJSON) {
// do something
someJSON[key]
}
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
let iterable = "boo";
for (let value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"
let iterable = new Map([
["a", 1],
["b", 2],
["c", 3],
]);
for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]
for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3
let iterable = new Set([1, 1, 2, 2, 3, 3]);
for (let value of iterable) {
console.log(value);
}
// 1
// 2
// 3
function* fibonacci() {
// 생성기 함수
let [prev, curr] = [1, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (let n of fibonacci()) {
console.log(n);
// 1000에서 수열을 자름
if (n >= 1000) {
break;
}
}
// 2
// 3
// 5
// 8
// 13
// 21
// 34
// 55
// 89
// 144
// 233
// 377
// 610
// 987
// 1597
const numbers=[45, 4, 9, 16, 25];
let txt="";
function myFunction(value, index, array) {
txt+=value;
}
numbers.forEach(myFunction);
numbers.forEach((value) => {
console.log(value);
});
```/
## RRA
- stove99.tistory.com :: Java Map 반복(Iteration)시키는 3가지 방법, 2011-11-13
- stackoverflow.com :: How do I efficiently iterate over each entry in a Java Map?, 2008-09-05 asked by iMack, 2016-02-22 answered by Slava Vedenin
- 개발자를 위한 웹 기술 > JavaScript > JavaScript 참고서 > 문 및 선언 > for
- 개발자를 위한 웹 기술 > JavaScript > JavaScript 참고서 > 문 및 선언 > for...in
- 개발자를 위한 웹 기술 > JavaScript > JavaScript 참고서 > 문 및 선언 > for...of
- 개발자를 위한 웹 기술 > JavaScript > JavaScript 참고서 > 표준 내장 객체 > Array > Array.prototype.forEach()
'[IT/Programming]' Related Articles