-4

I want to store more than one elements in an index. Example I want to store 23,45,67 at index 0, 12 34 56 at index 1, and so on. How can I store?

Zaid Khan
  • 54
  • 1
  • 6
  • 1
    Have you ever heard of Arrays of Arrays. http://stackoverflow.com/questions/4781100/how-to-make-an-array-of-arrays-in-java – Yassin Hajaj Nov 07 '15 at 13:23
  • but I want to build.. That's my part of home work – Zaid Khan Nov 07 '15 at 13:24
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and https://docs.oracle.com/javase/8/docs/api/java/util/Map.html – AK_ Nov 08 '15 at 09:45

3 Answers3

2

May a two dimensional array will help?

int[][] yourArray = {{23,45,67},{0,12,34}};

you can access the values like this

yourArray[0][0]; //will be 23
yourArray[0][1]; //will be 45
yourArray[0][2]; //will be 67
yourArray[1][0]; //will be 0
yourArray[1][1]; //will be 12
yourArray[1][2]; //will be 34
Yannic Bürgmann
  • 6,301
  • 5
  • 43
  • 77
0

Try to build an ArrayList of ArrayLists, or something similar

0

There are a couple different ways you could try this. One option is a 2D array, basically an array of arrays, which is discussed in detail here. The best way I can think of is using something called a Linked List, which is a data structure that does not have a set numerical order, but instead each item of data also has a variable referring to the next item of data. There's lots of information here on StackOverflow on how to implement it. By creating an array of Linked Lists, index 0 would refer to a list, that could contain as many pieces of data as you would like. It's discussed in detail here. Best of luck!

Community
  • 1
  • 1
Katie O.
  • 11
  • 3