Results 1 to 7 of 7
  1. #1
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Efficiency of code...

    Hello, I have a simple question.

    I am just wondering out of curiosity, which of the following two methods for loading a series of numbers from a text file, converting them to an integer and loading them into an array is the most efficient?

    The input file looks like the following:

    11111111111111111111
    10000000000000000001
    10111111001000000001
    10100000001111111111
    10100011111000000001
    10100000001000100001
    10100000031000100001
    10111111111000100001
    10000000000000100021
    11111111111111111111

    Java Code:
    for (int i = 0; i < 10; i++) {
        temp = input.nextLine();
        for (int j = 0; j < 20; j++) {
    //     position[i][j] = Integer.parseInt(temp.charAt(j)+""); //       <--Method one
           position[i][j] = Character.getNumericValue(temp.charAt(j)); // <--Method two
        }
    }
    The first method I discovered by accident. When adding a string to a character, the resultant is a string. Method two uses a default char to int method found in the character class.

    Or is there some other, more efficient method of loading entire text files whose contents contain only numbers and the number of rows and columns are known (as in above, 10 rows, 20 columns) into a 2-D array of integers?

    Thanks in advance =).

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Inventor22 View Post
    Java Code:
    //     position[i][j] = Integer.parseInt(temp.charAt(j)+""); //       <--Method one
           position[i][j] = Character.getNumericValue(temp.charAt(j)); // <--Method two
    The first method constructs a String; you parse it and throw it away immediately afterwards; the second method is more efficient. Better yet, use the Character.digit( ... ) method if you're only interested in the arabic digits 0 ... 9.

    kind regards,

    Jos

  3. #3
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Smile

    Thank you! :)

  4. #4
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    If you know that there are ever Latin digits 0-9 then the efficient solution is:

    position[i][j] = temp.charAt(j) - '0';

    But if you expect the Arabic numbers also in digits other languages like Arabic, Hebrew, Thailand then you need to use one of the methods in the Character class.

    If the data are not already verified then you should also add an error handling.
    Volker Berlin
    www.inetsoftware.de

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Horcrux7 View Post
    If you know that there are ever Latin digits 0-9 then the efficient solution is: [snip]
    Just a minor nitpick: the digits 0 ... 9 are arabic digits, the romans used those funny i, v, x, l, c, d, m thingies ;-)

    kind regards,

    Jos

  6. #6
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    This is the typical confusion between Arabic numbers and Arabic digits.

    Arabic digits are '٠' ... '٩' and Latin Numbers are '0' ... '9'. Both are Arabic Numbers. But the Arabic people named it India numbers. If you was ever in any Arabic country then you will see that on the products (water, chocolate, etc. ) overall are Arabic digits and not Latin digits. The Arabic digits look more like the Arabic letters. Else the Latin digits look like more Latin Letters.

    This all has noting to do with the roman numbers.

    Edit: My browser (Firefox) show first the Arabic 9 and then Arabic 0. But this is wrong. The Arabic Digits will written in the same order like Latin. Only the Arabic Letters are written from right to left.
    Last edited by Horcrux7; 09-26-2010 at 09:06 AM.
    Volker Berlin
    www.inetsoftware.de

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Horcrux7 View Post
    This is the typical confusion between Arabic numbers and Arabic digits.
    It's more complex than that: there are the 'Eastern Arabic Numerals' and the 'Western Arabic Numerals' (the stuff we use); I was talking about the latter; also read this link.

    kind regards,

    Jos

Similar Threads

  1. URLConnection Efficiency
    By Lil_Aziz1 in forum New To Java
    Replies: 22
    Last Post: 08-19-2010, 06:27 PM
  2. Replies: 7
    Last Post: 07-29-2010, 11:25 AM
  3. An Efficiency Question
    By Revenna in forum Java 2D
    Replies: 0
    Last Post: 06-25-2010, 07:22 AM
  4. Replies: 2
    Last Post: 02-10-2010, 07:00 PM
  5. method efficiency
    By TheWave in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •