2012-03-05 174 views
5

我想要學習如何做的是將一個int數組轉換爲一個int在C#中。如何將int數組轉換爲int?

但是我想追加int數組中的值。

實施例:

int[] array = {5, 6, 2, 4}; 

將被轉換成等於5624.

感謝預先任何幫助一個int。

+3

如果數組中的數字包含一個無法放入'int'的數字,會發生什麼? – Jon 2012-03-05 10:08:24

+1

那些不在0 ... 9範圍內的數組項呢? – 2012-05-24 08:46:40

回答

13

只需將每個數字乘以10 ^他在數組中的位置即可。

int[] array = { 5, 6, 2, 4 }; 
int finalScore = 0; 
for (int i = 0; i < array.Length; i++) 
{ 
    finalScore += array[i] * Convert.ToInt32(Math.Pow(10, array.Length-i-1)); 
} 
+2

爲了更快一點Convert.ToInt32(Math.Pow(10,array。(int k = 0; k 2012-03-05 10:27:23

2

嘗試以下操作:

 int[] intArray = new int[] { 5, 4, 6, 1, 6, 8 }; 

     int total = 0; 
     for (int i = 0; i < intArray.Length; i++) 
     { 
      int index = intArray.Length - i - 1; 
      total += ((int)Math.Pow(10, index)) * intArray[i]; 
     } 
+0

它返回'4265' - 這不是期望的結果。 – 2012-03-05 10:16:41

+0

對不起,你是對的,編輯我的答案。 – 2012-03-05 10:20:11

0

這將是容易,如果你理解了十進制系統是如何工作的。

因此,讓我解釋一下,對於你來說:一個十進制數字包含以十爲底的單個數字。

這意味着你必須通過此數組迭代(向後!)和10^

相乘有關示例5624是指:(5×10^3)+(6×10^2)+(2 * 10^1)+(4 * 10^0)

也請考慮:http://en.wikipedia.org/wiki/Horner_scheme

2

使用此代碼,你只是想連接你的int數組所以使用下面的代碼

String a; 
int output; 
int[] array = {5, 6, 2, 4}; 
foreach(int test in array) 
{ 
a+=test.toString(); 
} 
output=int.parse(a); 
//where output gives you desire out put 

這不是e xact代碼。

+0

-1不是一個好的解決方案,字符串操作很慢。 – 2012-03-05 10:21:36

+3

@ FelixK。 OP沒有指定他需要時間敏感的代碼,這是最易讀的編碼答案。 – ediblecode 2012-03-05 10:36:54

+0

儘管如此,Shadow Wizard的答案更具可讀性和安全性,但重要的是,與其他解決方案相比,它在速度方面不是一個好的解決方案。 – 2012-03-05 10:43:37

4

另一種簡單的方法:這裏

int[] array = {5, 6, 2, 4}; 
int num; 
if (Int32.TryParse(string.Join("", array), out num)) 
{ 
    //success - handle the number 
} 
else 
{ 
    //failed - too many digits in the array 
} 

訣竅是使陣列的數字串然後解析它作爲整數。

+1

最簡單! +1。不是最好的.. – nawfal 2012-11-13 15:43:54

+0

@nawfal謝謝,我認爲[這一個](http://stackoverflow.com/a/9564827/447356)是最好的,upvoted它自己。 :) – 2012-11-13 16:01:18

+0

我愛你。如果效率不重要,這看起來最酷http://stackoverflow.com/a/9567257/661933 – nawfal 2012-11-13 16:13:02

0

這將做到這一點:

public int DoConvert(int[] arr) 
{ 

    int result = 0; 

    for (int i=0;i<arr.Length;i++) 
    result += arr[i] * Math.Pow(10, (arr.Length-1)-i); 

    return result; 
} 
2
int result = 0; 
int[] arr = { 1, 2, 3, 4}; 
int multipicator = 1; 
for (int i = arr.Length - 1; i >= 0; i--) 
{ 
    result += arr[i] * multipicator; 
    multipicator *= 10; 
} 
0

而只是爲了好玩...

arr.Select((item, index) => new { Item = item, Power = arr.Length - (index - 1) }).ToList().ForEach(item => total += (int)(Math.Pow(10, item.Power) * item.Item)); 
6
int output = array 
    .Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1))) 
    .Sum(); 
+1

+1這很酷 – Richard 2012-03-05 13:17:05

0
var finalScore = int.Parse(array 
    .Select(x => x.ToString()) 
    .Aggregate((prev, next) => prev + next)); 
1

你可以使用字符串流 (包括 「sstream」 )

using namespace std; int main(){

int arr[3]={3,2,4};  //your array.. 

stringstream ss; 

ss<<arr[0]; //this can be run as a loop 
ss<<arr[1]; 
ss<<arr[2]; 


int x; 
ss>>x; 

cout<<x;  //simply the int arr[3] will be converted to int x.. 
+0

記得添加** #include ** – 2014-02-22 18:20:46