Android defines one-dimensional arrays in the resource file (res/strings. xml) and indirectly defines two-dimensional arrays, androidstrings. xml

Source: Internet
Author: User

Android defines one-dimensional arrays in the resource file (res/strings. xml) and indirectly defines two-dimensional arrays, androidstrings. xml

We often define strings and one-dimensional arrays in resource files (res/strings. xml). What are the two-dimensional arrays? The two-dimensional array cannot be directly defined and can be indirectly defined.

In fact, it is very simple. You can remember it once. One-dimensional arrays are often used, but two-dimensional arrays should be less useful, because only two-dimensional arrays can be indirectly defined.

Array definition:

An array is a set of variable array definitions that define the same data type at a time.

Array features:

1. arrays are collections of elements of the same data type. 2. Elements in the array are sequentially stored in the memory sequentially. 3. the array element is represented by the name of the entire array and its sequential position in the array. For example, a [0] indicates the first element in the array named a, and a [1] indicates the second element of array.

First, let's see how the string is defined. The method for adding comments is as follows: <! -- Comment content -->

<! -- String --> <string name = "hello_world"> Hello world! </String>

Of course, this is defined in java: String a = "Hello world"; or String s = new String ("abcdefghijklmnopqrstuvwxyz ");

1. One-dimensional array

<! -- One-dimensional array --> <string-array name = "good"> <item> a </item> <item> B </item> <item> c </item> <item> d </item> </string-array>

Get the array method in the resource using java code

Resources res = getResources (); // retrieve the String [] good = res. getStringArray (R. array. good) character array in xml file format );

By the way, let's take a look at how to define in java: String [] s = {"a", "B", "c", "d "};

2. Two-dimensional array

So far, do you know how to define a two-dimensional array? Do you still have to think about it? Actually, you cannot define a two-dimensional array directly in the resource file, therefore, we can only obtain two-dimensional arrays in an indirect way. Multi-dimensional arrays are also indirectly defined for acquisition.

<! -- Indirectly defines a two-dimensional array using a one-dimensional array --> <array name = "two"> <item> a, B, c, d, e, f, g </item> <item> h, I, g, k, l, m, n </item> </array>

Parse the preceding one-dimensional array as a two-dimensional array using java

Resources res = getResources (); // String [] array = res. getStringArray (R. array. two); String [] [] result = getTwoDimensionalArray (array ); /*** resolve a one-dimensional array as a two-dimensional array according to the set rules * @ 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 ;}

Here, we provide only one method, so that you can use it in international languages. Obviously, this process of parsing is much slower. Alternatively, you can use other methods instead of two-dimensional arrays. You may think it is better to define it directly in java:

Similar to int a [2] [3] = {1, 2, 3}, {4, 5, 6 }};

Here is just a way to provide, not the best, maybe you still have better, please leave a message.

Link: http://www.cnblogs.com/liqw/p/4181327.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.