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

  • SEARCH
  • Home
  • 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 8109109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:19:06+00:00 2026-06-06T01:19:06+00:00

In a java program, I am getting some data with each value being assigned

  • 0

In a java program, I am getting some data with each value being assigned a name-
Each data item has a ‘level’ which is a numeric value.

For level 1, there are ‘n’ number of values–
the data items have names

 1-1, 1-2, 1-3.....1-n.

Now, level 2 items have names which are derived from level 1’s data items.

So there can be ‘m’ values of level 2, corresponding to each level 1 item. In the name of each data item, each named section corresponding to a level, is separated from other section of name with a ‘~’.

For eg,

    1-1~2-1, 1-1~2-2, 1-1~2-3......1-1~2-m --->level2 corresp. to level 1 item "1-1"
    1-2~2-1, 1-2~2-2, 1-2~2-3, ....1-2~2-m--->level2 corresp. to level 1 item "1-2"

Now, I want to create a function where a parameter is number of levels, and I want to programmatically access all items of that level.

For eg, I can create a for loop within another for loop, to access items of level 2.

But how do I do this for level=x, where x is a variable?

  • 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-06-06T01:19:08+00:00Added an answer on June 6, 2026 at 1:19 am

    The simplest approach is to use recursion as well as a loop.

    public static int count(List list) {
      int sum = 0;
      for(Object o: list) {
         sum += o instanceof List ? count((List) o) : 1;
      }
      return sum;
    }
    

    As you can see, this will iterate through every element at every level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program (running in JDK 1.5 for now) which is getting
I need help getting my java program ready for release. I have a program
Why am I getting java.io.FileNotFoundException in the following program? import java.io.*; class FisDemo {
A simple java program to input the name of student and find the average
I am writing a Java program which automatically plugs information into a website, which
Java memory model gives DRF guarantee(Data race freedom) which means that a data race
I have a Java program that executes Runtime.getRuntime().exec(ls -l); many times, once for each
I am getting some problem while executing binary executable file using Java code. My
I am connecting oracle db thru java program. The problem is i am getting
I am trying to migrate a java application which consist of some graph charts

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.