我最近发现java.lang.String.substring方法不返回一个新字符串,而是返回一个包含原始字符串的视图.这可能会对记忆产生影响.例如,如果您正在读取ascii文件,并使用子字符串解析文件中的标记并将子字符串的结果存储在内存中 - 您实际存储在内存中的是子字符串操作之前的整个字符串!您当然可以通过在自己的版本中包装子字符串来解决此问题,该子字符串返回子字符串结果的新字符串.
我正在使用Visual Studio在Windows上编写c/c ++代码.我想知道如何有效地计算我的过程的开始时间.我可以使用gettimeofday()吗?我从谷歌找到了以下代码,但我不明白它在做什么:
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
//I'm lost at this point
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
/*converting file time to unix epoch*/
tmpres /= 10; /*convert into microseconds*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = …Run Code Online (Sandbox Code Playgroud) 当我运行以下程序时:
object Problem {
def main(args: Array[String]) = {
val v = 53.toString
val w = v(0).toInt
println(w)
}
}
Run Code Online (Sandbox Code Playgroud)
它打印出来53,而不是我所期望的5.有人可以帮我理解为什么吗?
更新:如果我使用charAt而不是数组语法,会发生同样的事情
在下面的示例中,我尝试使用sun.tools.javac.Main动态编译我生成的类,然后实例化该类的对象并调用方法.到目前为止,我甚至无法通过加载生成的类.我在Eclipse中得到以下异常:
java.lang.ClassNotFoundException: TestHello_1289950330167
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at MyClassGenerator.runIt(MyClassGenerator.java:47)
at MyClassGenerator.main(MyClassGenerator.java:13)
Note: sun.tools.javac.Main has been deprecated.
1 warning
Running TestHello_1289950330167:
Run Code Online (Sandbox Code Playgroud)
这是代码:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.net.URLClassLoader;
public class MyClassGenerator {
String generatedClassName = "TestHello_" + System.currentTimeMillis();
String javaFileName = this.generatedClassName + ".java";
public static void main(final String args[]) {
final …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我使用动态生成一个类sun.tools.javac.Main.我将使用Reflection创建此类的新实例.问题是,我想避免使用Reflection来调用我为这个类定义的方法,所以我创建了一个ProxyInvoker,它引用了我在项目中定义的接口.为了让类加载器看到这一点,我将类路径添加到我的类加载器的Executable接口.在"编译"步骤中,我仍然收到一条错误,指出我的界面未找到.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class MyClassGenerator {
static final String generatedClassName = "TestHello_" + System.currentTimeMillis();
static final String javaFileName = generatedClassName + ".java";
static URLClassLoader classLoader;
public static void main(final String args[])
throws MalformedURLException {
final ProxyInvoker proxy = new ProxyInvoker();
generateClass();
loadExecutableInterface();
if (compileClass()) {
System.out.println("Running " + generatedClassName + ":\n\n");
final Executable ex = createExecutable();
ex.execute();
}
else {
System.out.println(javaFileName + " is bad.");
}
} …Run Code Online (Sandbox Code Playgroud) 我在Scala中编写了这个基本程序:
import scala.collection.mutable.HashMap
object HelloWorld {
val treasureMap = new HashMap[BigInt, BigInt]
def main(args: Array[String]) {
println(fibCache(10))
}
def fibCache(n: BigInt): BigInt = {
if (n == 0 || n == 1) {
return n
}
return treasureMap.getOrElseUpdate(n, fibCache(n - 1) + fibCache(n - 2))
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有很大的值,我会有一个OutOfMemoryError或者什么,但我看到了这个:
Exception in thread "main" java.lang.StackOverflowError
at java.math.BigInteger.compareMagnitude(Unknown Source)
at java.math.BigInteger.compareTo(Unknown Source)
at scala.math.BigInt.compare(BigInt.scala:141)
at scala.math.BigInt.$less$eq(BigInt.scala:145)
at scala.math.BigInt.fitsInLong(BigInt.scala:130)
at scala.math.BigInt.hashCode(BigInt.scala:120)
at scala.runtime.BoxesRunTime.hashFromNumber(Unknown Source)
at scala.collection.mutable.HashTable$HashUtils$class.elemHashCode(HashTable.scala:366)
at scala.collection.mutable.HashMap.elemHashCode(HashMap.scala:43)
at scala.collection.mutable.HashTable$class.findEntry(HashTable.scala:108)
at scala.collection.mutable.HashMap.findEntry(HashMap.scala:43)
at scala.collection.mutable.HashMap.get(HashMap.scala:63) …Run Code Online (Sandbox Code Playgroud) 可以说我有以下代码:
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.createStatement(myQueryString);
rs = ps.executeQuery();
// process the results...
} catch (java.sql.SQLException e) {
log.error("an error!", e);
throw new Exception("I'm sorry. Your query did not work.");
} finally {
ps.close(); // if we forgot to do this we leak
rs.close(); // if we forgot to do this we leak
}
Run Code Online (Sandbox Code Playgroud)
我希望能够抓住我忘记关闭PreparedStatement或ResultSet使用Checkstyles的情况.这是可能的,如果是的话,我该怎么做呢?
我的日志文件中有以下条目:
09-22-11 12:35:09 1ms INFO ...erChangeSetListener:91 11 processing changeSet for class:4328,at version:1316720109100
09-22-11 12:35:09 779ms INFO ...erChangeSetListener:91 11 processing changeSet for class:4334,at version:1316720109882
09-22-11 12:35:09 1ms INFO ...erChangeSetListener:91 11 processing changeSet for class:4328,at version:1316720109882
09-22-11 12:35:11 1s WARNING QueryServiceImpl:100 - no existing index for class:4328
09-22-11 12:35:11 SEVERE QueryRequest:107 7 Aod query resulted in error:No index available for class:4328
09-22-11 12:35:11 SEVERE AuthenticationTask:48 - EndUserException: an error occurred when processing the query Dump: /data1/amir/dev/devots/logs/dumps/22i123511.dump
Run Code Online (Sandbox Code Playgroud)
我正在应用此正则表达式:
final String pattern = "^(\\d{2}-\\d{2}-\\d{2} …Run Code Online (Sandbox Code Playgroud) 我正在回顾Googles Guava API的功能,我遇到了一个我在"真实世界编程"体验中没有看到过的数据结构,即BiMap.对于给定值,这种构造的唯一好处是能够快速检索密钥吗?是否存在使用BiMap最佳表达解决方案的问题?
我正在尝试创建一个简单的会话缓存,允许我在整个缓存上设置最大大小.缓存不是计算缓存,所以我认为Guava CacheBuilder与a LoadingCache不合适.我希望能够执行以下操作:
get(key)来自缓存的数据,该数据在存储操作
put(key,value)期间在存储操作期间被存储到缓存中
我尝试使用MapMaker,但最大尺寸方法似乎过时了.有谁知道我有什么选择?我总是可以使用一个简单的地图并按照我自己的政策滚动?
java ×7
guava ×2
scala ×2
bimap ×1
c++ ×1
caching ×1
checkstyle ×1
classloader ×1
jdbc ×1
process ×1
reflection ×1
regex ×1
substring ×1
time ×1
windows ×1