2015-02-09 64 views
1

在PHP字符串操作,它是可以操縱的字符串像我們想,例如:陣列中的Java

$temp2 = array(); 
$theindex = 0; 
foreach($arraysimpanloop as $a) { 
    $temp1 = array(); 
    for($i = 0; $i < sizeof($a)-1; $i++) { 
     $temp1[$i] = $a[$i]; 
    } 
    natsort($temp1); // sort array temp1 
    $stringimplode = implode($temp1); 
    $temp2[$theindex] = $stringimplode; 
    $theindex++; 
} 

由於我使用的是現在,Java,所以我不知道怎麼行:

$temp2[$theindex] = $stringimplode; 

用Java代碼編寫。因爲我在java中試過:

temp2[theindex]= stImplode; // here temp2 is array of string 

它總是返回一個空指針。

+0

你有沒有初始化數組第一?就我個人而言,我懷疑該行拋出一個NullPointerException。你能展示你的Java代碼和堆棧跟蹤嗎? – Stultuske 2015-02-09 07:52:00

+1

基於你在這裏向我們展示的一行代碼:'temp2 [theindex] = stImplode;' - 我們無法幫到你。如果您希望我們幫助您獲得NPE,請發佈您的代碼。 – alfasin 2015-02-09 07:54:02

+2

請添加'java'代碼。 – Jens 2015-02-09 07:54:27

回答

0

您的字符串數組可能尚未初始化

int yourSize = ...; 
String[] temp2 = new String[yourSize]; 

這就是爲什麼你可能會得到空指針exeception。 或者你不存在數組訪問元素(例如,您的數組有100個元素,並且您嘗試訪問101元)

+0

感謝您的幫助,甚至有點不同,但現在我找到了解決方案! – user3698011 2015-02-09 10:06:45

0
// You must create array objects prior to use (as your array() does) 
String[] temp = new String[]{ "one", "two", "three" }; 
String[] a = new String[3]; 

// not trying to do anything meaningful 
a[0] = "111"; 
a[1] = "222"; 
a[2] = "333"; 
int i = 0; 
for(String e: a){ 
    temp[i++] = e; 
} 
for(int i = 0; i < temp.length; ++i){ 
    System.out.println(temp[i]); 
} 

檢查Java數組一些教程...