Android defines a one-dimensional array in a resource file (Res/strings.xml), indirectly defining a two-dimensional array

Source: Internet
Author: User

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--

    <!--strings--    <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

    <!--one-dimensional arrays--    <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

Resources Res =getresources ();//An array of characters in the XML file format 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.

    <!--indirectly define a two-dimensional array with one-dimensional arrays--<array name= "II" >        <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

Resources Res =getresources ();//An array of characters in the XML file format string[] array = Res.getstringarray (R.array.two); String[][] result = Gettwodimensionalarray (array);/** * Resolves one-dimensional array to a two-dimensional array by setting rules * @param array * @return */private string[][] Get Twodimensionalarray (string[] array) {string[][] Twodimensionalarray = null;for (int i = 0; i < Array.Length; i++) {Stri ng[] 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.

This article link: http://www.cnblogs.com/liqw/p/4181327.html

Android defines a one-dimensional array in a resource file (Res/strings.xml), indirectly defining a two-dimensional array

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.