English 中文(简体)
退出法时,瓦 Java阵列就失去了。
原标题:Java array is lost when exiting method

我比较新,不提点人通过,使我困惑不解。 我写了家庭工作功能,要求我恢复用户投入的长度,并将使用投入分配给一个阵列,因为这种方法消失了用户投入阵列,这是错误的。

public static int readArray(char[] intoArray)
    {
        char[] capture = captureInputAsCharArray(); //User input comes back as char[]
        System.arraycopy(intoArray,0, capture, 0, capture.length);

        return capture.length;
    }

public static main(String[] args)
{
        size = readArray(arrItem7);  // item 7
        System.out.println(size);
        printOneInLine(arrItem7);  // prints individual elements of array
}
最佳回答

由于你有以下论点:<编码>System.arraycopy(后退。

rel=“noretinger”http://download.oracle.com/javase/6/docs/api/java/lang/System.html

public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)

Swap intoArraycapture:

System.arraycopy(capture,0, intoArray, 0, capture.length);
问题回答

为了做你想要做的工作(针对用户的投入和重新确定其规模),你可以这样做:

import java.util.*;


class Main{
  public static void main(String argv[])
  {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter something");
    String line = sc.nextLine();
    char [] my_array = line.toCharArray();
    System.out.println("You entered an input of length "+line.length());
  }

}

And it will give this:

$ java Main 
Enter something
Hello
You entered an input of length 5

参考本身具有价值。 在此情况下,您应回填(capture)。 本身不是长度。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签