excuse me -2 请问如何动态生成数组,java不会没有方法实现吧?
这个和excuse me 一样,犯
java.lang.NullPointerException import java.io.*;
public class two_eight {
public static void main (String[] args)
{
int list[]=null;
list=get();
for(int i=0;i <list.length;i++){
System.out.print(list[i]);
}
}
static int Search(int a[],int i,int left,int right)
{
while(left <right)
{
int middle = (left+right)/2;
if(middle==a[middle])return middle;
else if(middle> a[middle])left=middle+1;
else right=middle-1;
}
return -1;
}
static int[] get()
{
int[] array=null;
/*System.out.println( "Please enter num ");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
int n = -1;
try {
n = Integer.parseInt(stdin.readLine());
} catch (
NumberFormatException e) {
e.printStackTrace();
} catch (
IOException e) {
e.printStackTrace();
} */
for(int i=0;i <3;i++)
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try {
array[i] = (int)Integer.parseInt(stdin.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return array;
}
}
------解决方案--------------------....动态数组用容器
或者看看这个动态扩展数组的方法..
public static Object arrayGrows(Object oldArray) {
Class cl = oldArray.getClass();
if(!cl.isArray()) return null;
Class componentType = cl.getComponentType();
int length = Array.getLength(oldArray);
int newLength = length * 11/10 + 10;
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(oldArray, 0, newArray, 0, length);
return newArray;
}
------解决方案--------------------