将输入读取为数组

发布于 2024-09-28 16:57:15 字数 447 浏览 1 评论 0原文

当我输入“read 1 2 3 4”时,我想从输入流读取一些内容并将其存储在 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
   }
}

I want to make something read from inputstream to store in an int[] when I type "read 1 2 3 4". what should i do?

I do not know the size of the array, everything is dynamic...

Here is the current code:

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
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-10-05 16:57:16

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

You either use a storing structure with nodes, that you can easily append one after another, or, if you really must use arrays, you need to allocate space periodically, as it becomes necessary.

心是晴朗的。 2024-10-05 16:57:16

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

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 ] );

}

Parse-out the data and keyword from your string then push it into something like this:

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 ] );

}
故笙诉离歌 2024-10-05 16:57:15

您需要构建一些东西来解析输入流。假设它实际上像您所指出的那样不复杂,您需要做的第一件事就是从 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();

或者您可以使用 BufferedReader< /code> (正如注释所建议的):

BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
String line = rdr.readLine();

一旦你有一行要处理,你需要将它分成几部分,然后将这些部分处理成所需的数组:

// 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]);
    }
}

我确信其中一些方法可以抛出异常(至少 readparseInt 做),我将把处理这些作为练习。

You need to build something to parse the input stream. Assuming it's literally as uncomplex as you've indicated the first thing you need to do is get the line out of the InputStream, you can do that like this:

// 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();

Or you can use a BufferedReader (as suggested by comments):

BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
String line = rdr.readLine();

Once you have a line to process you need to split it into pieces, then process the pieces into the desired array:

// 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]);
    }
}

I'm sure some of these methods can throw exceptions (at least read and parseInt do), I'll leave handling those as an exercise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文