Last active
April 21, 2020 11:57
-
-
Save nakov/8ebbd98c22915e5b261d0bb1a8f11608 to your computer and use it in GitHub Desktop.
Arrays and Lists in C#, Java, Python and JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// List examples in JavaScript | |
let incomes = [150, 200, 70.50, 120]; | |
// Print the list (naturally) | |
console.log(incomes); | |
// Print the list (using foreach loop) | |
console.log("Incomes:"); | |
for (let x of incomes) { | |
console.log(" ", x) | |
} | |
console.log(); | |
// Add a new element | |
incomes.push(300); | |
console.log("Added 300"); | |
console.log("Incomes:", incomes); | |
console.log(); | |
// Calculate count, sum, average, first, last, ... | |
console.log("Count:", incomes.length); | |
console.log("First:", incomes[0]); | |
console.log("Second:", incomes[1]); | |
console.log("Last:", incomes[incomes.length-1]); | |
console.log("Sum:", incomes.reduce((a,b) => a + b, 0)); | |
console.log("Avg:", incomes.reduce((a,b) => a + b, 0) / incomes.length); | |
console.log(); | |
// Remove elements at position 1, 2 and 3 | |
incomes.splice(1, 3); | |
console.log("Deleted 3 elements starting at index 1"); | |
console.log("Incomes:", incomes); | |
console.log(); | |
// Modify an existing element | |
incomes[0] = incomes[0] * 1.2; | |
console.log("Increased the first income with 20%"); | |
console.log("Incomes:", incomes); | |
console.log(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List examples in Python | |
incomes = [150, 200, 70.50, 120] | |
# Print the list using a fore-loop | |
print("Incomes:") | |
for x in incomes: | |
print(" ", x) | |
print() | |
# Add a new element | |
incomes.append(300) | |
print("Added 300") | |
print("Incomes:", incomes) | |
print() | |
# Calculate count, sum, average, first, last, ... | |
print("Count:", len(incomes)) | |
print("First:", incomes[0]) | |
print("Second:", incomes[1]) | |
print("Last:", incomes[-1]) | |
print("Sum:", sum(incomes)) | |
print("Avg:", sum(incomes) / len(incomes)) | |
print() | |
# Remove elements at position 1, 2 and 3 | |
incomes = incomes[:1] + incomes[4:] | |
print("Deleted 3 elements starting at index 1") | |
print("Incomes:", incomes) | |
print() | |
# Modify an existing element | |
incomes[0] = incomes[0] * 1.2 | |
print("Increased the first income with 20%") | |
print("Incomes:", incomes) | |
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// List examples in C# | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class ListExamples | |
{ | |
static void Main() | |
{ | |
var incomes = new List<double> { | |
150, 200, 70.50, 120 | |
}; | |
// Print the list using a foreach-loop | |
Console.WriteLine("Incomes:"); | |
foreach (var amount in incomes) | |
{ | |
Console.WriteLine(amount); | |
} | |
Console.WriteLine(); | |
// Add a new element | |
incomes.Add(300); | |
Console.WriteLine("Added 300"); | |
// Print the list using String.Join() | |
Console.WriteLine("Incomes: {0}\n", | |
String.Join(", ", incomes)); | |
// Calculate count, sum, average, first, last, ... | |
Console.WriteLine("Count = {0}", incomes.Count); | |
Console.WriteLine("First = {0}", incomes.First()); | |
Console.WriteLine("Second = {0}", incomes.Skip(1).First()); | |
Console.WriteLine("Last = {0}", incomes.Last()); | |
Console.WriteLine("Sum = {0}", incomes.Sum()); | |
Console.WriteLine("Avg = {0}", incomes.Average()); | |
Console.WriteLine(); | |
// Remove elements at position 1, 2 and 3 | |
incomes.RemoveRange(1, 3); | |
Console.WriteLine("Deleted 3 elements starting at index 1"); | |
// Modify an existing element | |
incomes[0] = incomes[0] * 1.20; | |
Console.WriteLine("Increased the first income with 20%"); | |
// Print the list using a for-loop | |
Console.WriteLine("Incomes:"); | |
for (var index = 0; index < incomes.Count; index++) | |
{ | |
Console.WriteLine(incomes[index]); | |
} | |
Console.WriteLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// List examples in Java | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.stream.Collectors; | |
public class ListExamples { | |
public static void main(String[] args) { | |
var incomes = new ArrayList<Double>( | |
Arrays.asList(150.0, 200.0, 70.50, 120.0) | |
); | |
// Print the list using a foreach-loop | |
System.out.println("Incomes:"); | |
for (var amount : incomes) | |
System.out.println(amount); | |
System.out.println(); | |
// Add a new element | |
incomes.add(300.0); | |
System.out.println("Added 300"); | |
System.out.println("Incomes: " + incomes); | |
// Print the list using Collectors.joining() | |
System.out.printf("Incomes: %s\n\n", | |
incomes.stream() | |
.map(x -> String.valueOf(x)) | |
.collect(Collectors.joining(", "))); | |
// Calculate count, sum, average | |
System.out.printf("Count = %d\n", incomes.size()); | |
System.out.printf("First = %s\n", incomes.stream().findFirst().get()); | |
System.out.printf("First = %s\n", incomes.get(0)); | |
System.out.printf("Second = %s\n", incomes.stream().skip(1).findFirst().get()); | |
System.out.printf("Second = %s\n", incomes.get(1)); | |
System.out.printf("Last = %s\n", incomes.stream().skip(incomes.size() - 1).findFirst().get()); | |
System.out.printf("Last = %s\n", incomes.get(incomes.size() - 1)); | |
System.out.printf("Sum = %s\n", incomes.stream().mapToDouble(Number::doubleValue).sum()); | |
System.out.printf("Avg = %s\n", incomes.stream().mapToDouble(Number::doubleValue).average().getAsDouble()); | |
System.out.println(); | |
// Remove elements at position 1, 2 and 3 | |
incomes.subList(1, 4).clear(); | |
System.out.println("Deleted 3 elements starting at index 1"); | |
System.out.println(incomes); | |
// Modify an existing element | |
incomes.set(0, incomes.get(0) * 1.20); | |
System.out.println("Increased the first income with 20%\n"); | |
// Print the list using a for-loop | |
System.out.println("Incomes:"); | |
for (var i = 0; i < incomes.size(); i++) | |
System.out.println(incomes.get(i)); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment