哪一个更优或者有什么不同?
String s = methodThatReturnsString();
int i = methodThatReturnsInt();
thirdMethod(s, i);
Run Code Online (Sandbox Code Playgroud)
要么
thirdMethod(methodThatReturnsString(), methodThatReturnsInt());
Run Code Online (Sandbox Code Playgroud)
通过最优,我的意思是在内存使用等方面最优.
我正在使用gzip压缩字符串,然后解压缩结果,但是我得到了以下异常,为什么?
output: Exception in thread "main" java.util.zip.ZipException: oversubscribed dynamic bit lengths tree at java.util.zip.InflaterInputStream.read(Unknown Source) at java.util.zip.GZIPInputStream.read(Unknown Source) at Test.main(Test.java:25)
public class Test {
public static void main(String[]args) throws IOException{
String s="helloworldthisisatestandtestonlydsafsdafdsfdsafadsfdsfsdfdsfdsfdsfdsfsadfdsfdasfassdfdfdsfdsdssdfdsfdsfd";
byte[]bs=s.getBytes();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(outstream);
gzipOut.write(bs);
gzipOut.finish();
String out=outstream.toString();
System.out.println(out);
System.out.println(out.length());
ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes());
GZIPInputStream gzipIn=new GZIPInputStream(in);
byte[]uncompressed = new byte[100000];
int len=10, offset=0, totalLen=0;
while((len = gzipIn.read(uncompressed, offset, len)) >0){ // this line
offset+=len;
totalLen+=len;
}
String uncompressedString=new String(uncompressed,0,totalLen); …Run Code Online (Sandbox Code Playgroud) 我保存到会话的对象是a LocalizationContext,它不可序列化,我的Tomcat是5.5.28,而qa服务器是Tomcat 5.5.30.这是来自Tomcat文档:
每当Catalina正常关闭并重新启动,或者触发应用程序重新加载时,标准的Manager实现将尝试将所有当前活动的会话序列化为通过pathname属性定位的磁盘文件.然后,当应用程序重新加载完成时,所有这些保存的会话将被反序列化并激活(假设它们在平均时间内没有过期).
为了成功恢复会话属性的状态,所有这些属性必须实现java.io.Serializable接口.您可以通过
<distributable>在Web应用程序部署描述符(/WEB-INF/web.xml)中包含该元素,使Manager强制实施此限制 .
这让我觉得它应该打破我的本地,并且你永远不能在Tomcat中保存会话属性,除非它实现Serializable.
编辑:
对不起,为了更清楚,我的问题是双重的.是否所有会话属性都必须是可序列化的,如果这样做,为什么它仍然可以在我的本地环境中工作?
我想在类上访问静态方法,但是要在泛型中传递该类.
我做了以下事情:
class Base{
public static String getStaticName(){
return "Base";
}
}
class Child extends Base{
public static String getStaticName(){
return "Child";
}
}
class StaticAccessor{
public static <T extends Base>String getName(Class<T> clazz){
return T.getStaticName();
}
}
StaticAccessor.getName() // --> "Base"
Run Code Online (Sandbox Code Playgroud)
这将返回"基地",但我想要的是"孩子"任何人没有反思的建议吗?
我明白这是基本的东西的一部分,但我被卡住了:-(有人可以帮帮我吗?
计划1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=1,b=2,c;
c=(a+b)++;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出错误?是否需要左值?
计划2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *p1="name";
char *p2;
p2=(char*)malloc(20);
memset(p2,0,20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
Run Code Online (Sandbox Code Playgroud)
为什么是输出,一个空字符串?如果我颠倒增量的顺序,那就是:while(++*p2=++*p1);为什么左值误差来了?
我有一个ArrayList的String[].
每个String[]包含两个值:值和日期.
如果我打印它,它看起来像这样:
value | date -------|---------- 357.0 | 2011/05/30 -234.0 | 2011/05/31 -123.0 | 2011/05/30
我想创建一个新的ArrayList,在同一日期的所有值相加.像这样:
value | date -------|---------- 234.0 | 2011/05/30 -234.0 | 2011/05/31
有人可以帮忙吗?
我正在尝试运行从Sun Java站点获取的代码(我没有复制它,查看它并编写它,因为它可以帮助我记住代码).
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharEx{
FileReader inputStream = null;
FileWriter outputStream = null;
public static void main(String args[]) throws IOException{
FileReader inputStream = null;
FileWriter outputStream = null;
try{
inputStream = FileReader("xanadu.txt");
outputStream = FileWriter("out.txt");
int c;
while ((c = inputStream.read()) != -1){
outputStream(c);
}
}
finally{
if(inputStream !=null){
inputStream.close();
}
if(outputStream !=null){
outputStream.close();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我遇到了错误.
D:\Java>javac CharEx.java
CharEx.java:14: cannot find symbol
symbol : method FileReader(java.lang.String)
location: class CharEx
inputStream = …Run Code Online (Sandbox Code Playgroud) 有没有办法删除Java中的所有字母或数字?
例如,
123$32 -> 12332
1234 abcd /n -> 1234abcd
Run Code Online (Sandbox Code Playgroud) java ×9
io ×2
arraylist ×1
arrays ×1
c ×1
filereader ×1
filewriter ×1
generics ×1
gzip ×1
httpsession ×1
lvalue ×1
methods ×1
optimization ×1
pointers ×1
session ×1
static ×1
tomcat ×1