Let’s solve Day 21: Generics. Generics is a powerful concept in programming that allows you to write reusable code that can work with different data types. Join us on this exciting journey to explore generics and their practical application.😍
Task
Write a single generic function named printArray; this function must take an array of generic elements as a parameter (except for C++, which takes a vector). The locked Solution class in your editor tests your function.
Note: You must use generics to solve this challenge. Do not write overloaded functions.
Explanation
To solve this challenge, we need to implement a generic function called printArray that can accept and print arrays of different data types. Generics allow us to write code that can work with multiple types without duplicating code for each type.
The printArray function should iterate over the elements of the input array and print them one by one. By using generics, we can ensure that the function works seamlessly with arrays of different types, such as integers, strings, or custom objects.
Day 21: Generics Solution In C++
template <typename T>
void printArray(const vector<T> &arr) {
for (const auto &elem : arr) {
cout << elem << endl;
}
}
int main() {
int intCount;
cin >> intCount;
vector<int> intArr(intCount);
for (int i = 0; i < intCount; ++i) {
cin >> intArr[i];
}
int stringCount;
cin >> stringCount;
vector<string> stringArr(stringCount);
for (int i = 0; i < stringCount; ++i) {
cin >> stringArr[i];
}
printArray(intArr);
printArray(stringArr);
return 0;
}
Day 21: Generics Solution In Java
public class Solution {
public static <T> void printArray(ArrayList<T> arr) {
for (T elem : arr) {
System.out.println(elem);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int intCount = scanner.nextInt();
ArrayList<Integer> intArr = new ArrayList<>();
for (int i = 0; i < intCount; ++i) {
intArr.add(scanner.nextInt());
}
int stringCount = scanner.nextInt();
ArrayList<String> stringArr = new ArrayList<>();
for (int i = 0; i < stringCount; ++i) {
stringArr.add(scanner.next());
}
System.out.println("Int Array:");
printArray(intArr);
System.out.println("String Array:");
printArray(stringArr);
scanner.close();
}
}
Learning from this Question
By solving the “Day 21: Generics” challenge, you can learn several important concepts:
- Generics: You will gain a deep understanding of generics and their use in writing reusable and type-safe code.
- Type parameters: You will learn how to define and use type parameters in generic functions or classes.
- Code reusability: You will see how generics promote code reusability by allowing you to write code that can work with multiple types.
- Type safety: You will understand how generics improve type safety by catching type mismatches at compile-time.
- Problem-solving skills: You will enhance your problem-solving skills by designing and implementing a solution that meets the given requirements.
Conclusion
The “Day 21: Generics” challenge introduces the concept of generics and their practical application in writing reusable code. By implementing the printArray function using generics, you will gain valuable experience in working with type parameters and leveraging the power of generics to write code that can work with multiple data types. The complete code for this challenge is available in my GitHub repository✨.
This exercise reinforces the importance of generics in software development, as they promote code reusability, type safety, and maintainability⭐.
“Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.”
– Patrick McKenzie