Monday, December 19, 2022

Iterate list, set and map in java, python 3, c++ and javascript

List

Here is how you can enumerate a list in each of the languages:

Java:

import java.util.ArrayList; 
import java.util.List; 
public class Main
    public static void main(String[] args)
        List<String> names = new ArrayList<>(); 
        names.add("Alice"); 
        names.add("Bob"); 
        names.add("Charlie"); 
        for (int i = 0; i < names.size(); i++) { 
            System.out.println(i + ": " + names.get(i)); 
        
    
}

Python 3:

names = ['Alice', 'Bob', 'Charlie'
for i, name in enumerate(names): 
    print(f'{i}: {name}')

C++:

#include <iostream> 
#include <vector> 
int main()
    std::vector<std::string> names = {"Alice", "Bob", "Charlie"}; 
    for (int i = 0; i < names.size(); i++) { 
        std::cout << i << ": " << names[i] << std::endl
    
    return 0
}

JavaScript:

const names = ['Alice', 'Bob', 'Charlie']; 
names.forEach((name, i) =>
    console.log(`${i}: ${name}`); 
});

Set


Here is how you can iterate a set in each of the languages:

Java:

import java.util.HashSet
import java.util.Set
public class Main
    public static void main(String[] args) { 
        Set<String> names = new HashSet<>(); 
        names.add("Alice"); 
        names.add("Bob"); 
        names.add("Charlie"); 
        for (String name : names) { 
            System.out.println(name); 
        
    
}

Python 3:

names = {'Alice', 'Bob', 'Charlie'
for name in names: 
    print(name)

C++:

#include <iostream> 
#include <set> 
int main()
    std::set<std::string> names = {"Alice", "Bob", "Charlie"}; 
    for (const auto& name : names) { 
        std::cout << name << std::endl
    
    return 0
}

JavaScript:

const names = new Set(['Alice', 'Bob', 'Charlie']); 
for (const name of names) { 
    console.log(name); 
}

I hope this helps! Let me know if you have any questions.Here is how you can iterate a set in each of the languages you mentioned:

Java:

import java.util.HashSet
import java.util.Set
public class Main
    public static void main(String[] args) { 
        Set<String> names = new HashSet<>(); 
        names.add("Alice"); 
        names.add("Bob"); 
        names.add("Charlie"); 
        for (String name : names) { 
            System.out.println(name); 
        
    
}

Python 3:

names = {'Alice', 'Bob', 'Charlie'}     
for name in names: 
    print(name)

C++:

#include <iostream> 
#include <set> 
int main()
    std::set<std::string> names = {"Alice", "Bob", "Charlie"}; 
    for (const auto& name : names) { 
        std::cout << name << std::endl
    
    return 0
}

JavaScript:

const names = new Set(['Alice', 'Bob', 'Charlie']); 
for (const name of names) { 
    console.log(name); 
}

Map


Here is how you can iterate a map in each of the languages you mentioned:

Java:

import java.util.HashMap
import java.util.Map
public class Main
    public static void main(String[] args) { 
        Map<String, Integer> ages = new HashMap<>(); 
        ages.put("Alice", 30); 
        ages.put("Bob", 35); 
        ages.put("Charlie", 40); 
        for (Map.Entry<String, Integer> entry : ages.entrySet()) {                       System.out.println(entry.getKey() + " is " + entry.getValue() + " years old"); 
        
    
}

Python 3:

ages = {'Alice': 30, 'Bob': 35, 'Charlie': 40
for name, age in ages.items(): 
    print(f'{name} is {age} years old')

C++:

#include <iostream> 
#include <map> 
int main()
    std::map<std::string, int> ages = {{"Alice", 30}, {"Bob", 35}, {"Charlie", 40}}; 
    for (const auto& [name, age] : ages) { 
        std::cout << name << " is " << age << " years old" << std::endl
    
    return 0
}

JavaScript:

const ages = { Alice: 30, Bob: 35, Charlie: 40 }; 
for (const [name, age] of Object.entries(ages)) { 
    console.log(`${name} is ${age} years old`); 
}

I hope this helps! Let me know if you have any questions.

No comments:

Post a Comment

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...