Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Bidirectional Student Data Traversal with Built-in Iterators in Java and C++

Tech 1

This experiment demonstrates how to travrese a collection of student records using native iterator mechanisms in both Java and C++. The dataset consists of ten students, each with an ID, name, and age. The goal is to output the records sorted by ID in ascending and descending order, leveraging language-specific container types and their associated iteration protocols.

Java Impleemntation

Java uses the List interface backed by ArrayList, sorted via Collections.sort() with a lambda-based Comparator. Iteration proceeds through the enhanced for-loop (syntactic sugar over Iterator), ensuring clean, readable traversal without manual iterator management.

import java.util.*;

class Person {
    final int studentId;
    final String fullName;
    final int yearsOld;

    Person(int id, String name, int age) {
        this.studentId = id;
        this.fullName = name;
        this.yearsOld = age;
    }
}

public class StudentTraversal {
    public static void displayAll(List<Person> roster) {
        for (Person p : roster) {
            System.out.printf("ID: %d, Name: %s, Age: %d%n", 
                p.studentId, p.fullName, p.yearsOld);
        }
    }

    public static void main(String[] args) {
        List<Person> cohort = new ArrayList<>(Arrays.asList(
            new Person(20220001, "Zhang San", 20),
            new Person(20220002, "Li Si", 21),
            new Person(20220003, "Wang Wu", 22),
            new Person(20220004, "Zhao Liu", 23),
            new Person(20220005, "Zhou Qi", 19),
            new Person(20220006, "Wu Ba", 20),
            new Person(20220007, "Zheng Jiu", 21),
            new Person(20220008, "Wang Shi", 22),
            new Person(20220009, "Feng Shiyi", 23),
            new Person(20220010, "Chen Shier", 20)
        ));

        // Ascending order by ID
        cohort.sort(Comparator.comparingInt(p -> p.studentId));
        System.out.println("Ascending by ID:");
        displayAll(cohort);

        // Descending order by ID
        cohort.sort(Comparator.comparingInt(p -> -p.studentId));
        System.out.println("\nDescending by ID:");
        displayAll(cohort);
    }
}

C++ Implementation

C++ employs std::vector<Person> and leverages std::sort with custom lambdas for ordering. Range-based for-loops provide iterator abstraction while maintaining full compatibility with STL’s iterator concepts. The Person class uses member initialization lists for clarity and efficiency.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

struct Person {
    int studentId;
    std::string fullName;
    int yearsOld;

    Person(int id, std::string name, int age)
        : studentId{id}, fullName{std::move(name)}, yearsOld{age} {}
};

void printRoster(const std::vector<Person>& roster, const char* label) {
    std::cout << label << "\n";
    for (const auto& p : roster) {
        std::cout << "ID: " << p.studentId
                  << ", Name: " << p.fullName
                  << ", Age: " << p.yearsOld << '\n';
    }
}

int main() {
    std::vector<Person> cohort;
    cohort.reserve(10);

    cohort.emplace_back(20220001, "Zhang San", 20);
    cohort.emplace_back(20220002, "Li Si", 21);
    cohort.emplace_back(20220003, "Wang Wu", 22);
    cohort.emplace_back(20220004, "Zhao Liu", 23);
    cohort.emplace_back(20220005, "Zhou Qi", 19);
    cohort.emplace_back(20220006, "Wu Ba", 20);
    cohort.emplace_back(20220007, "Zheng Jiu", 21);
    cohort.emplace_back(20220008, "Wang Shi", 22);
    cohort.emplace_back(20220009, "Feng Shiyi", 23);
    cohort.emplace_back(20220010, "Chen Shier", 20);

    // Sort ascending
    std::sort(cohort.begin(), cohort.end(), 
        [](const Person& a, const Person& b) { return a.studentId < b.studentId; });
    printRoster(cohort, "Ascending by ID:");

    // Sort descending
    std::sort(cohort.begin(), cohort.end(), 
        [](const Person& a, const Person& b) { return a.studentId > b.studentId; });
    printRoster(cohort, "\nDescending by ID:");

    return 0;
}
``>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.