Java 以数组形式读取输入

Java 以数组形式读取输入,java,input,Java,Input,当我键入“read1 2 3 4”时,我想将从inputstream读取的内容存储在int[]中。我该怎么办 我不知道数组的大小,一切都是动态的 以下是当前代码: BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); String line = stdin.readLine(); StringTokenizer st = new StringTokenizer(line); String com

当我键入“read1 2 3 4”时,我想将从inputstream读取的内容存储在int[]中。我该怎么办

我不知道数组的大小,一切都是动态的

以下是当前代码:

BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
StringTokenizer st = new StringTokenizer(line);
String command = st.nextToken();

if (command.equals("read")) {
   while (st.nextToken() != null) {
       //my problem is no sure the array size
   }
}

您可以使用带有节点的存储结构,这样可以轻松地一个接一个地追加,或者,如果确实必须使用数组,则需要在必要时定期分配空间。

您需要构建一些东西来解析输入流。假设它确实像您所指出的那样不复杂,那么您需要做的第一件事就是将该行从
InputStream
中取出,您可以这样做:

// InputStream in = ...;

// read and accrue characters until the linebreak
StringBuilder sb = new StringBuilder();
int c;
while((c = in.read()) != -1 && c != '\n'){
    sb.append(c);
}

String line = sb.toString();
public static Integer[] StringToIntVec( String aValue )
{

    ArrayList<Integer> aTransit = new ArrayList<Integer>();

    for ( String aString : aValue.split( "\\ ") )
    {
        aTransit.add( Integer.parseInt( aString ) );

    }

    return aTransit.toArray( new Integer[ 0 ] );

}
或者您可以使用
BufferedReader
(如注释所示):

一旦有一条线要处理,您需要将其拆分为多个部分,然后将这些部分处理为所需的阵列:

// now process the whole input
String[] parts = line.split("\\s");

// only if the direction is to read the input
if("read".equals(parts[0])){
    // create an array to hold the ints
    // note that we dynamically size the array based on the
    // the length of `parts`, which contains an array of the form
    // ["read", "1", "2", "3", ...], so it has size 1 more than required
    // to hold the integers, thus, we create a new array of
    // same size as `parts`, less 1.
    int[] inputInts = new int[parts.length-1];

    // iterate through the string pieces we have
    for(int i = 1; i < parts.length; i++){
         // and convert them to integers.
        inputInts[i-1] = Integer.parseInt(parts[i]);
    }
}
//现在处理整个输入
String[]parts=line.split(\\s”);
//仅当方向是读取输入时
如果(“读取”。等于(第[0]部分){
//创建一个数组来保存整数
//请注意,我们根据
//“parts”的长度,它包含表单的数组
//[“read”、“1”、“2”、“3”、…],因此它的大小比要求的大1
//因此,为了保存整数,我们创建了一个新的
//与“零件”尺寸相同,小于1。
int[]输入=新的int[parts.length-1];
//遍历我们拥有的字符串片段
对于(int i=1;i

我确信其中一些方法会抛出异常(至少
read
parseInt
do),我将把处理这些异常作为一个练习。

从字符串中解析出数据和关键字,然后将其推入如下内容:

// InputStream in = ...;

// read and accrue characters until the linebreak
StringBuilder sb = new StringBuilder();
int c;
while((c = in.read()) != -1 && c != '\n'){
    sb.append(c);
}

String line = sb.toString();
public static Integer[] StringToIntVec( String aValue )
{

    ArrayList<Integer> aTransit = new ArrayList<Integer>();

    for ( String aString : aValue.split( "\\ ") )
    {
        aTransit.add( Integer.parseInt( aString ) );

    }

    return aTransit.toArray( new Integer[ 0 ] );

}
public静态整数[]StringToIntVec(字符串aValue)
{
ArrayList aTransit=新的ArrayList();
用于(字符串aString:aValue.split(“\\”)
{
add(Integer.parseInt(aString));
}
返回ransit.toArray(新整数[0]);
}

使用BufferedReader的readLine()方法可以减少一些工作量。@shinkou,当然,这并不完全重要,但我会让你感到幽默。检查这个。