Often we will define a string in a resource file (Res/strings.xml), a one-dimensional array, which defines a two-dimensional array? The direct definition of a two-dimensional array is not found and can be defined indirectly.
In fact, it's easy to remember that one-dimensional arrays are often used, but two-dimensional arrays should be less useful because they can only indirectly define two-dimensional arrays.
Definition of the array:
An array is a set of variable array definitions that define the same data type at once.
Features of the array:
1. An array is a collection of elements of the same data type. 2. The elements in the array are sequential, and they are stored sequentially in memory in this order. 3. The array element is represented by the name of the entire array and its own order position in the array. For example, A[0] represents the first element in an array named A, A[1] represents the second element of array A.
Let 's take a look at how the string is defined. where comments are added:<!--comments--
?
12 |
<!-- 字符串 --> <string name= "hello_world" >Hello world!</string> |
This is, of course, defined in Java: String a = "Hello world"; or string s = new String ("abcdefghijklmnopqrstuvwxyz");
One or one-D arrays
?
1234567 |
<!-- 一维数组 --> <string-array name= "good" >
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item> </string-array> |
Getting the array method in the Java code for the resource
?
123 |
Resources res =getResources(); // 取xml文件格式的字符数组 String[] good=res.getStringArray(R.array.good); |
By the way, see how it is defined in Java: string[] s = {"A", "B", "C", "D"};
Two or two-D arrays
Here, you know how to define a two-dimensional array, is not to think about it, in fact, it is not directly in the resource file to define a two-dimensional array, so you can only use the indirect way to obtain a two-dimensional array, multidimensional array as such indirectly to define the acquisition.
?
12345 |
<!-- 用一维数组的方式间接定义二维数组 --> <array name= "two" >
<item>a,b,c,d,e,f,g</item>
<item>h,i,g,k,l,m,n</item>
</array> |
Parse the above one-dimensional array with Java as a two-dimensional array
?
1234567891011121314151617181920212223 |
Resources res =getResources();
// 取xml文件格式的字符数组
String[] array = res.getStringArray(R.array.two);
String[][] result = getTwoDimensionalArray(array);
/**
* 按设定规则解析一维数组为二维数组
* @param array
* @return
*/
private
String[][] getTwoDimensionalArray(String[] array) {
String[][] twoDimensionalArray =
null
;
for
(
int i =
0
; i < array.length; i++) {
String[] tempArray = array[i].split(
","
);
if
(twoDimensionalArray ==
null
) {
twoDimensionalArray =
new String[array.length][tempArray.length];
}
for
(
int j =
0
; j < tempArray.length; j++) {
twoDimensionalArray[i][j] = tempArray[j];
}
}
return
twoDimensionalArray;
}
|
It's just a way of doing it, so you can use it when you're international and multilingual. Obviously, this is an analytic process, much slower. Or it can be done without a two-dimensional array in the design, in other ways. Perhaps you would think it would be nice to define it directly in Java:
similar to int a[2][3]={{1,2,3},{4,5,6}};
Here is just to provide a way, not the best, perhaps you have better, please leave a message.
Other great article articles
jquery Tutorial (-dom) content setter and getter method for tree Operations Android Learning notes (37) using Datepickerdialog, Timepickerdialogandroid Learning Notes (36) Creating a custom dialog box with Alertdialog jquery Tutorial (1)-manipulating DOM operations Properties Spring MVC Beginner (11)-Other ways to return a JSON string
More about Android development articles
Android defines a one-dimensional array in a resource file (Res/strings.xml), indirectly defining a two-dimensional array