2015-02-09 66 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数组一些教程...