-1

I have created an array under

public class program {
   public static String[] tasks = new String[5];
}

Now I want to create a two dimensional String array with that existing String array (tasks) and a new String array (date) in order to have synchronized data.

How would one go about that in Java?

spedicey
  • 37
  • 7

6 Answers6

3

Can you explain more on the problem. This can be simply achieved using pojo. I couldn't comment so added it as ans.

You can covert array to list. Can this example is helpful.
What is the use of Collections.synchronizedList() method? It doesn't seem to synchronize the list

OR

public String[][] arr = new String[5][2]; // not sure why you need static
for (int i = 0; i < 5; i++) { // you can take constant or array length
   arr[i][0] = new Date().toString(); // or System milliseconds as string
   arr[i][1] = tasks[0];
}
shaILU
  • 2,050
  • 3
  • 21
  • 40
Not Avail
  • 101
  • 5
1

Like so

public static String[][] tasksWithDate = new String[task.length][2];

You can push your old data with a simple loop

for (int i = 0; i < task.length; i++) {
   taskWithDate[i][0] = tasks[0];
}

If you access the new array you can access a specfific task via taskWithDate[i][0] and the date with taskWithDate[i][1]

Zacki
  • 177
  • 11
1

First, a 2D array is an array of arrays. So to create a two dimensional array, you do it like so:

String[][] tasks = new String[5][2];

This creates a 2D array with 5 rows and 2 columns. To insert a record into the first row and first column, you do it like so:

tasks[0][0] = "My new string";

To loop through a 2D array, you will need 2 loops.

for (int row = 0; row < tasks.length; row++) { 
     
     for (int col = 0; col < tasks[row].length; col++) { 
       tasks[row][col] = "New string for index row " + row + " col " + col; 
     } 
}
Paul Tofunmi
  • 435
  • 4
  • 15
1

Below example will give you complete context.

  1. Adding Tasks with some values

  2. Creating Two Dimension array, where first index will present tasks value and 2nd index is Date.

  3. Printing all values.

    public static void method() {
    
    String[] tasks = { "1", "2", "3", "4", "5" };//new String[5];
    
    String[][] taskTwoDim = new String[tasks.length][2];
    
      //Adding values to two dimension array, 0->tasks, 1->String Date
      for (int i = 0; i < tasks.length; i++) {
          taskTwoDim[i][0] = tasks[i];
          taskTwoDim[i][1] = new Date().toString();
      }
      //Printing all values which is present on index-0, index-1
      for (int a = 0; a < taskTwoDim.length; a++) {
          System.out.println(taskTwoDim[a][0] + " " + taskTwoDim[a][1]);
      }
    }
    
Anurag Jain
  • 204
  • 1
  • 5
  • Why would you write new Date().toString();? Meaning what effect would it have on the output / the data that is inserted? – spedicey Dec 20 '20 at 12:36
  • As I understood your question, If you are asking something else then can you please give some more details, So I can help accordingly, Thanks!! In terms of synchronized it will impact as you are keeping date over here, If you want to keep time based synchronized data then, Use time in millisecond with some counter value as addition, like that if value is added at same millisecond time then also it will be synchronized or you can say unique as you are adding new key there. – Anurag Jain Dec 20 '20 at 12:55
0

If I understand your question correctly, it is better to create a new POJO class TaskDate.

public class program {
    
    public static TaskDate[] tasks = new TaskDate[5];

}

class TaskDate {
    private String task;
    private String date;

    // todo add a constructor, getters, and setters
}
djm.im
  • 3,295
  • 4
  • 30
  • 45
0

If I understand your question right, you can create a 2d array from 2 one-dimensional arrays by simply doing this:

int[][] resultArray = {task, date};