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 8010999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:54:12+00:00 2026-06-04T18:54:12+00:00

In Java, arrays of different dimensionalities have different types. So a method that takes

  • 0

In Java, arrays of different dimensionalities have different types. So a method that takes int[] as a parameter cannot take int[][] or int[][][]. I have a lot of code where I create methods that are quite similar but for the dimensionality of the array. Is there a way to handle arrays of arbitrary dimensionality, and thus abstract out this common functionality?

  • 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-04T18:54:13+00:00Added an answer on June 4, 2026 at 6:54 pm

    If you are willing to forego type safety, you can do it with a little recursion (no surprise here, right?) and reflection.

    The idea is to write your method in a way that it recurses down until the array has only one dimension. Once you’re at the single-dimension level, do the work; otherwise, call yourself recursively, and aggregate your findings from the prior levels if necessary.

    Here is a quick demo:

    import java.util.*;
    import java.lang.*;
    import java.lang.reflect.Array;
    
    class Main {
        public static int sumArray(Object array) {
                Class type = array.getClass();
                if (!type.isArray()) {
                        throw new IllegalArgumentException("array");
                }
                Class ct = type.getComponentType();
                int res = 0;
                int len = Array.getLength(array);
                if (ct.isArray()) {
                        for (int i = 0 ; i != len ; i++) {
                                res += sumArray(Array.get(array, i));
                        }
                } else {
                        for (int i = 0 ; i != len ; i++) {
                                res += Array.getInt(array, i);
                        }
                }
                return res;
        }
        public static void main (String[] args) throws java.lang.Exception
        {
                int[] a = new int[] {1,2,3,4,5,6,7,8,9,10};
                int aa[][] = new int[][] {{1,2},{3,4},{5,6}};
                int aaa[][][] = new int[][][]{{{1,2},{3,4},{5,6}},{{7,8},{9,10},{11,12}}};
                System.out.println(sumArray(a));
                System.out.println(sumArray(aa));
                System.out.println(sumArray(aaa));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why java Arrays use two different sort algorithms for different types? So
This code compiles: import java.io.Serializable; import java.util.Arrays; class Test<T extends Arrays & Serializable> {
Hi i'm writing a program thats using 2 dimensional int arrays in java. whenever
Its known that Java ArrayList is implemented using arrays and initializes with capacity of
I have the following issue: A java object contains two arrays of core datastore
I'm trying to code an application which runs un different java platforms like J2SE,
I have written a java program that tests the speed of a couple of
There are a lot of different classes that can be used in various ways
Hello i have a app which makes 3 wheels from 3 different arrays of
(Using Java) I'm testing sorting arrays to see literally how fast different sorting arrays

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.