Android中的Java函数调用和返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21949983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java function calling and return value in Android
提问by Brendon
I have a Java class with some code to be operated based on flag value, below is my code which works as flag value is 1
我有一个 Java 类,其中包含一些要根据标志值进行操作的代码,下面是我的代码,当标志值为 1 时
if(flag==1)
{
Log.d("Flag value", "flag= "+flag);
System.out.println("Read have "+read());
String tt=read();
s1=tt;
}
From this above function the value in the variable "s1" is some string value returned by read() function.
从上面的函数中,变量“s1”中的值是 read() 函数返回的某个字符串值。
the output of this code is returning two times of read() function, like
此代码的输出返回 read() 函数的两次,例如
s1 having "StringString"
s1 有“StringString”
Here is my read function code
这是我的读取功能代码
public String read(){
try{
FileInputStream fin = openFileInput(file);
int c;
while( (c = fin.read()) != -1)
{
temp = temp + Character.toString((char)c);
}
}
catch(Exception e)
{
}
Log.d("INSIDE READ FUNC", "temp have "+temp);
return temp;
}
While I omitted this "System.out.println("Read have "+read());" by below code
虽然我省略了这个 "System.out.println("Read have "+read());" 通过下面的代码
if(flag==1)
{
Log.d("Flag value", "flag= "+flag);
//System.out.println("Read have "+read());
String tt=read();
s1=tt;
}
And I got the perfect output like
我得到了完美的输出
s1 having "String"
s1 有“字符串”
How come the code works like this? I called the read() function only once to store to "tt" variable.
代码怎么会这样?我只调用了一次 read() 函数来存储到“tt”变量。
And storing the tt variable to s1 variable.
并将 tt 变量存储到 s1 变量。
But when I use System.out.println("Read have "+read()); its invoking and storing the returned string value in array and in the second time I am storing to the "tt" String variable and its appending the last returned string from the read() function to the "tt" String variable.
但是当我使用 System.out.println("Read have "+read()); 它调用并将返回的字符串值存储在数组中,第二次我将存储到“tt”字符串变量,并将最后一个从 read() 函数返回的字符串附加到“tt”字符串变量。
So the "tt" String variable having two times of read() function Returned String. How it is storing two times?
所以“tt”字符串变量有两次 read() 函数返回的字符串。它是如何存储两次的?
采纳答案by Biraj Zalavadia
if(flag==1)
{
Log.d("Flag value", "flag= "+flag);
//System.out.println("Read have "+read());
String tt=read();
s1=tt;
}
in above code read()
method is calls twice. And inside read()
method variable "temp"is declared global and you are concat the data like
在上面的代码read()
方法中调用了两次。并且内部read()
方法变量“temp”被声明为全局变量,并且您正在连接数据,例如
temp = temp + Character.toString((char)c);
so value is concat twice in temp variable.
所以值在临时变量中连接两次。
To Resolve the issue declare temp as local variable like
解决这个问题,将 temp 声明为局部变量,如
public String read(){
String temp="";
try{
FileInputStream fin = openFileInput(file);
int c;
while( (c = fin.read()) != -1)
{
temp = temp + Character.toString((char)c);
}
}
catch(Exception e)
{
}
Log.d("INSIDE READ FUNC", "temp have "+temp);
return temp;
}
回答by David C Adams
temp = temp + Character.toString((char)c);
You don't define temp in the read() method, so it is probably defined as a global variable. This means that every time you call the read() method, you are appending the new values to it. You should probably define temp in your read() method:
你没有在 read() 方法中定义 temp,所以它可能被定义为一个全局变量。这意味着每次调用 read() 方法时,都会向其附加新值。您可能应该在 read() 方法中定义 temp:
String temp;
that should fix it.
那应该解决它。
回答by Hitesh Singh
InputStream is = Context.openFileInput(someFileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((int bytesRead = is.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();