-2

I am trying to create an array of arrays so that i can have an array of years that breaks down into an array of 12 months that then can be broken down into 31 days. I am not sure if this process is doable in java or not, just trying to find a good method. thanks

Jacob
  • 1
  • 2
    If you know what you're trying to do, can you show us in psudeo-code or another language besides Java? Your description is pretty vague. – markspace Dec 02 '14 at 20:57
  • seems like a duplicate of this thread: http://stackoverflow.com/questions/4781100/how-to-make-an-array-of-arrays-in-java – Ensar Hatipoglu Dec 02 '14 at 21:02
  • Have a look at: [Java Tutorial - Arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – Jesper Dec 02 '14 at 21:03
  • Welcome to Stackoverflow Jacob. Have a read of the [**help section**](http://stackoverflow.com/help), under the **Asking** header for tips on asking questions and exactly what questions are appropriate here. Vague questions like this one tend to get closed if it is unclear what you are asking. – Rudi Kershaw Dec 02 '14 at 21:13

3 Answers3

1

You have to be way more specific about what you want to do, or what you have done, or any of that. But because you asked about collections of collections

You can use a multi-dimensional array. http://www.java2s.com/Tutorial/Java/0140__Collections/ArrayOfstringArrays.htm

String[][] cartoons = {
    { "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" },
    { "Rubbles", "Barney", "Betty", "Bam Bam" },
    { "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" },
    { "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" } };

Or Check out the API for ArrayList: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

This is how you would use one though.

 ArrayList<String[]> year= new ArrayList<>();
 String[] february = new String[28];

 year.add(february);

Though you need to be more specific about what you want to do. Because using collections to handle temporal data is a fools errand.

DejaVuSansMono
  • 787
  • 5
  • 14
0

You could simply create a two(or more) dimensional array

int[][] array = new int [5][12];

This would be an array of 5 years, each with 12 months. You could go even further to make a three dimensional array (picture a cube) or even a four dimensional array (I'll let you think about that one :))

See this post: Syntax for creating a two-dimensional array

Community
  • 1
  • 1
nLee
  • 1,320
  • 2
  • 10
  • 21
0

You can represent a date as an array of arrays like so:

    int date [][][] = new int [2015][12][31];

    for(int y = 0; y < date.length;y++){
        for(int m = 0; m < date[0].length;m++){
            for(int d = 0; d < date[0][0].length;d++){
                date[y][m][d] = d+1;
            }
        }
    }

But why? To access a specific date, you need the indices, but if you know the indices, you already know the date. And some of the values of this 'calendar' are invalid, e.g. 11-31,02-30,...

Gren
  • 1,850
  • 1
  • 11
  • 16