-5

I am really new to java and before this I have coded in Matlab. Now, I need to make a matrix (so 2d array in java) which stores the values for a set of actions lets say a = 1,...A for every time period t = 1,2...T. Hence, at the end I need a 2D array of where every row represents time t and every column represents an action.

I have been reading bunch of stuff on staclflow and my limited understanding tells me that may be what I need is linkedlist.

However, I am not sure that is the right way to go. So, I have two questions:

  1. What is the most efficient way to make a 2d array in JAVA?
  2. If you could please point me towards some reading material that can teach me how to add, remove, get and iterate over the elements of the method suggested by you.

I will appreciate any help.

Many thanks..!!!!

Amir Sagiv
  • 376
  • 5
  • 23

4 Answers4

1

An Array More than one dimension called 2d array.

int[][] arr1 = new int[4][2]; here 4 rows and 2 columns get created
String[][] locations = new String[3][3]; here 3 rows and 3 columns get created
int[] [] arr2=new int[][]; // here it is not ok bcz you must specify parent node size

here we see best example of 2d array is..

class TestArray
{
public static void main(String[] args)
{
     String[][] employeeNames= 
{{ "Mr. John" , "Mr. Smith" , " Mr. James" },{"Test Engineer","Developer"} };
System.out.println(employeeNames[0][0]+employeeNames[1][0]); // Mr. John Test Engineer
}
}
Rajesh
  • 642
  • 2
  • 7
  • 14
0

If you could please point me towards some reading material that can teach me how to add, remove, get and iterate over the elements of the method suggested by you.

Arrays don't support those operations. To add and remove elements you really need to use a List such as ArrayList.

List<List<Integer>> list2d = new ArrayList<>();
// add a row
list2d.add(new ArrayList<>());
// add an element
list2d.get(0).add(1);

Note: if you wish to lookup by date you can use a Map of LocalDate such as

Map<LocalDate, List<Double>> dateToDoubleList = new HashMap<>();

There are a number of 3rd party libraries which support List<double> more efficiently. an ArrayList<Double> can use 28 bytes per element and a LinkedlIst can use 48 bytes per entry. If you use a collection which wraps primitives (or write your own) it might only use 8 bytes per double.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks a lot for pointing me towards list. In fact, this is what I asked in the question: if Linkedlist is right or not? My rows are represented by time which is an integer value and columns by actions which is double. And, this is what was confusing me. I will try your suggestiion, thanks once again..!! – user3469181 May 02 '15 at 18:02
  • @user3469181 if you have lots of memory, it may not matter whether you use LinkedlIst or not. LinkedList will make random inserts and deletes faster, but slow down access and use much more memory. If you need to perform a lookup to find your data, perhaps a `Map` is useful as it can handle arbitrarily large `Integer` values as keys (as well are String or Double or Date) – Peter Lawrey May 02 '15 at 18:12
  • I did think of using Map but then how can i store a 2D array using a map.. ??? And, I got confused that how would I iterate through it...because for my understanding map has one key and it stores one value for that key. – user3469181 May 02 '15 at 18:16
  • Thanks for editing the comment, I will try to do what you suggested and then get back to you, thanks once again..!!! – user3469181 May 02 '15 at 18:22
  • @user3469181 you can iterate over a Map using an Iterator e.g. `for(Map.Entry> entry : map.entrySet()) { ...` – Peter Lawrey May 02 '15 at 18:23
0

Have you considered using a Map? They use keys and values to organize their contents. In your case, the key would be a time, and the values the actions that occurred at said time. This means if you give the Map a time, it will return the actions related to it.

Assuming you use Date for representing time, and a class called Action for the actions, the implementation would look like this:

Map<Date, List<Action>> timesWithActions = new HashMap<Date, LinkedList<Action>>();

Map is an interface, so we have to use one of its implementations, in this case a HashMap.

Martin M J
  • 830
  • 1
  • 5
  • 12
0

This question could be like this

What are the various ways to create 2-D or multi-D array in java

well, i know few ways i have tried. These are not limited. various ways are available.

int[][] multi = new int[5][10];

  • one bad way is like below , but it works.

int[] twoDimIntArray[] = new int[5][10];

You can even create a skewed two-dimensional array with each row,

 int array[][] = new int[3][];
array[0] = new int[3];
array[1] = new int[2];
array[2] = new int[5];

one more

int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};

Biyu
  • 513
  • 1
  • 5
  • 30