Change an int array to an int integer

Source: Internet
Author: User


An interview question:

The Java implementation transforms an int array into an int integer. For example: int[] arr = new int[]{1,2,3,4,5}; Becomes 12345


/** * created by leo on 15/10/27. */public class interview {     public static void main (String[] args)  {         system.out.println (parseint (new int[]{1,2,3,4,5}));         system.out.println (Integer.parseint ("12345"));    }     /**     * 12345     *  @param  arr      *  @return      */    public  static final int parseint (Int[] arr) {         int length = arr.length;        int total  = 0;        for  (int i = 0; i  < length; i++) {            int num = arr[i];             total = total +  Calculateweightnum (num, length -  (i + 1))  ;         }        return total;    }     private static int calculateweightnum (int num , int  Numoften) {        int result = num;         for  (int i = 0; numoften > 0 & & i < numoften; i++) {             result = result * 10;        }         return result;    }} 


This article is from the "Java Small Novices" blog, be sure to keep this source http://leokongwq.blog.51cto.com/1310215/1706710

Change an int array to an int integer

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.