I have a project I am working in in my comp sci class and I completely forgot how to add a String to a 2-dimensional array. Any help would be appreciated.
Asked
Active
Viewed 84 times
-3
-
1https://stackoverflow.com/questions/10751603/how-to-insert-values-in-two-dimensional-array-programmatically – SoConfused Jan 16 '19 at 19:53
-
Stack overflow isn't really a place to get solutions to homework questions. You need to come up with some code and ask for help on specific problems. – Gremash Jan 16 '19 at 19:54
-
1https://stackoverflow.com/questions/25051554/in-java-how-do-you-insert-a-string-into-a-2d-array-of-string – GBlodgett Jan 16 '19 at 20:02
-
Please see https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – GhostCat Jan 16 '19 at 20:07
3 Answers
-1
You can do this like below:
String[][] array = new String[2][2]; // Initializing 2x2 array that will contains Strings
array[0][0] = "Some text"; // Put String object into array at index 0-0
System.out.println(array[0][0]); // Print element from array at index 0-0

Tom
- 224
- 3
- 9
-2
Reading strings and store them to 2 dimensional array :
String[][] data=new String [10][10];
Scanner sc=new Scanner(System.in);
for(int i=0;i<data.length;i++){
for(int j=0;j<data[i].length;j++){
data[i][j]=sc.nextLine();
}
}
This will dynamically input the strings and store in 2D array.

ygbgames
- 191
- 9
-3
The answer depends on which programming language you're using. Generally speaking, you'll access the 2D
array through its rows and columns with indices. Then, to add an element to that location in the array, you'll simply assign a string to that particular location. For example, in Java
you'll do the following:
String[][] arr = new String[10][10];
arr[0][0] = "Element 0";

ttmtran
- 71
- 1
- 9