Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 286199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:36:53+00:00 2026-05-12T05:36:53+00:00

I’m often confronted with the following situation: I have some data in a table

  • 0

I’m often confronted with the following situation: I have some data in a table (nested arrays) and need to loop over all items in it.

Requirements:

  1. The first item should be identified and handled separately.
  2. The last item should be identified and handled separately.
  3. If similiar items are sorted they should be identified in groups with the first and last group item identified. (in this case sort/group by gender)
  4. Subtotals of group items and total of all items should be given.

DATA:
Comic characters with firstname, lastname, tvshow, gender
assuming to be ordered by gender, lastname.

Turanga|Leela|Futurama|Female
Marge|Simpson|The Simpsons|Female
Eric|Cartman|South Park|Male
Peter|Griffin|Family Guy|Male
Homer|Simpson|The Simpsons|Male

QUESTION:
How would you code a loop (any language welcome!) that produces the following output?
Indention is not important.

OUTPUT:

First Entry: Turanga Leele (Futurama)
--
First Female: Turanga Leela (Futurama)
Last Female : Marge Simpson (The Simpsons)
--
Total Females: 2
--
First Male: Eric Cartman (South Park)
            Peter Griffin (Family Guy)
Last Male : Homer Simpson (The Simpsons)
--
Total Males: 3
--
Last Entry: Homer Simpson (The Simpsons)
--
Total Entries: 5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:36:53+00:00Added an answer on May 12, 2026 at 5:36 am

    I picked the world’s most verbose language to do this in (Java), but this should do what you want. It requires only a single pass, but needs one row of look ahead. It also requires at least two lines of input although it would be relatively easy to adapt to any number of lines:

        import java.util.Arrays;
        import java.util.HashMap;
        import java.util.Iterator;
        import java.util.List;
        import java.util.Map;
    
        import static java.lang.System.out;
    
        public class Loop {
            static enum Col { FIRST_NAME, LAST_NAME, TV_SHOW, GENDER }
    
        private static final List<String[]> characters = Arrays.asList(
                new String[] {"Turanga", "Leela", "Futurama", "Female"},
                new String[] {"Marge", "Simpson", "The Simpsons", "Female"},
                new String[] {"Eric", "Cartman", "South Park", "Male"},
                new String[] {"Peter", "Griffin", "Family Guy", "Male"},
                new String[] {"Homer", "Simpson", "The Simpsons", "Male"});    
    
        public static void summarize(List<String[]> character, Col groupBy) {
            assert character.size() > 1;        
            int total = 0;
            Iterator<String[]> i = characters.iterator();
            String[] row = next(i);
            String[] peek = next(i);
            String group;
            out.print("First Entry:" + format(row));        
            Map<String, Integer> subTotals = new HashMap<String, Integer>();
            do {
                group = col(row, groupBy);
                out.print("First " + group + ":");            
                subTotals.put(group, 0);
                do {
                    out.print(format(row));
                    total = incrementTotals(total, group, subTotals);
                    row = peek;
                    peek = next(i);
                } while (peek != null && col(peek, groupBy).equals(group));
                total = incrementTotals(total, group, subTotals);
                out.print("Last " + group + ":" + format(row));
                out.println("--");
                out.println("Total " + group + "s:" + subTotals.get(group));
                out.println("--");
                if (peek == null) break;
                row = peek;
                peek = next(i);            
            } while(true);
            out.print("Last Entry:" + format(row));
            out.println("--");
            out.println("Total Entries:" + total);        
        }
    
    
        private static String[] next(Iterator<String[]> i) {
            if (i.hasNext())
                return i.next();
            return null;
        }
    
    
        private static int incrementTotals(int total, String group,
                Map<String, Integer> subTotals) {
            total++;
            subTotals.put(group, subTotals.get(group) + 1);
            return total;
        }
    
        private static String format(String[] row) {
            return col(row, Col.FIRST_NAME) + " " + col(row, Col.LAST_NAME)
                + "(" + col(row, Col.TV_SHOW) + ")\n";
        }
    
        private static String col(String[] row, Col col) {
            return row[col.ordinal()];
        }
    
        public static void main(String args[]) {
            summarize(characters, Col.GENDER);
        }
    
    }
    

    Output is:

    First Entry:Turanga Leela(Futurama)
    First Female:Turanga Leela(Futurama)
    Last Female:Marge Simpson(The Simpsons)
    --
    Total Females:2
    --
    First Male:Eric Cartman(South Park)
    Peter Griffin(Family Guy)
    Last Male:Homer Simpson(The Simpsons)
    --
    Total Males:3
    --
    Last Entry:Homer Simpson(The Simpsons)
    --
    Total Entries:5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 305k
  • Answers 305k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From what I been able to find out, the pthread… May 13, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer Try this: public class DisposeJFrame extends JFrame{ JPanel panel =… May 13, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer If you are sure you will NEVER EVER need or… May 13, 2026 at 9:03 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.