读一行文字.一条线被认为是由换行符('\n'),回车符('\ r')或回车符后面的任何一个终止,然后是换行符.------ javadoc 1.8
然后我有一个像这样的文本文件:
the first line
the second line
Run Code Online (Sandbox Code Playgroud)
注意: seond line的最后一个字符是'e',也就是说不存在回车符.
那么这是我的演示代码.
public void process() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("demo.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
Run Code Online (Sandbox Code Playgroud)
实际输出:
the first line
the second line
Run Code Online (Sandbox Code Playgroud)
那么我的问题是为什么readLine方法可以获得第二行,因为它没有行分隔符(\n或\ r或\n\r \n).
我知道存在一个文件结束(EOF),但似乎javadoc并没有告诉EOF也是明确的行分隔符.
如果我使用Scanner而不是BufferedReader,代码如下:
public void testScan() throws IOException{
Scanner scan = new Scanner(new FileInputStream("demo.txt"));
String line;
while((line=scan.nextLine())!=null){
System.out.println(line);
}
scan.close();
}
Run Code Online (Sandbox Code Playgroud)
然后输出将是:
the first line
the second line
Exception in thread "main" java.util.NoSuchElementException: No …Run Code Online (Sandbox Code Playgroud) 我找到了一个非常神奇的东西,简单的代码如下:
public class Demo{
public static void main(String[] args){
HashMap<String,String> map = new HashMap<String,String>();
map.put("a", "aa");
System.out.println("end");
}
}
Run Code Online (Sandbox Code Playgroud)
在调用之后
HashMap<String,String> map = new HashMap<String,String>();
Run Code Online (Sandbox Code Playgroud)
字段变量entrySet不为null,也就是说它已经初始化.
那么这是我的第一个问题,什么时候初始化了entrySet?似乎相关的代码应该在HashMap的构造中,但下面是这个构造函数的源代码
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
Run Code Online (Sandbox Code Playgroud)
它似乎不存在初始化entrySet的代码.
事情还在继续.在调用之后
map.put("a","aa")
Run Code Online (Sandbox Code Playgroud)
字段变量表和entrySet的内容如下图所示.
那么这是我的第二个问题:将此值添加到entrySet中时?似乎应该是put方法实现这些东西.以下是放方法.
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
Run Code Online (Sandbox Code Playgroud)
它调用putVal …
我尝试使用golang处理这个问题557. Reverse Words in a String III 我的代码如下:
import "fmt"
import ss "strings"
func reverseWords(s string) string {
words := ss.Split(s," ");
res := "";
for i:=0; i < len(words);i++{
curWord := ss.Split(words[i],"");
for j:=len(curWord)-1; j >= 0;j--{
res += curWord[j];
}
if(i!=len(words)-1){
res += " ";
}
}
return res;
}
func main(){
s := "Let's take LeetCode contest";
fmt.Println(reverseWords(s));
}
Run Code Online (Sandbox Code Playgroud)
我的电脑一切正常,至少可以通过编译。
但是,当我在 leetcode 中提交时,它告诉我:
Line 67: undefined: strings in strings.Trim
Run Code Online (Sandbox Code Playgroud)
我谷歌这个错误,但没有得到任何相关信息。作为golang的初学者,我需要帮助。一切都会受到赞赏。
我是Gradle新秀,我不确定Gradle在运行测试集时是否会启动新的JVM。
就像将JVM参数传递给Gradle测试任务一样,我想将一些参数传递给运行测试集的JVM。
我在build.gradle中添加了以下几行:
...
test {
groovy {
jvmArgs '-agentpath:/usr/lib/code_dependency_capturer.so' // add line
srcDirs = ['src/test']
if (!JavaVersion.current().isJava8Compatible()) {
exclude '**/v8/*'
exclude '**/vm8/*'
}
}
resources {
srcDirs = ['src/test-resources']
}
}
...
Run Code Online (Sandbox Code Playgroud)
但这告诉我:
A problem occurred evaluating root project 'groovy'.
Could not find method jvmArgs() for arguments[-agentpath:/usr/lib/code_dependency_capturer.so] on source set 'test' of type org.gradle.api.internal.tasks.DefaultSourceSet.
Run Code Online (Sandbox Code Playgroud)
我搜索了此错误,但未能解决。
我正在学习加载Java类的过程,遇到一些困惑。
我知道加载Java类时,当前classLoader不会直接加载Java类,它将委托给其父classLoader(一个递归过程),直到其父不能加载此类为止。
问题是?当前的classLoader是什么? 引导程序?延期?应用程式?
如何获取当前的classLoader?而且我知道有一个API:
xxx.Class.getClassLoader();
Run Code Online (Sandbox Code Playgroud)
但我不确定返回值是否为currentClassLoader。我认为应该是实际加载此java类的classLoader 。
为了更详细地描述我的问题,我将举一个例子。
我在博客中获得了以下内容。
ThreadContextClassLoader用于处理Java SPI,接口在Java中定义
core lib并由Bootstrap ClassLoader加载,第三方实现这些接口,然后由AppClassLoader加载jar
解决方案:传统的classLoader无法处理这种情况,因为它无法发现第三方jar当我们在核心库中使用第三方工具时。
我能理解的大多数上述方法都使我感到困惑:例如,接口CoreA和类CoreB在Java中core lib,应由Bootstrap ClassLoader加载,而AImpl是由第三方实现的A的实现,并应由以下方法加载AppClass加载器。
代码段如下:
public Interface CoreA{
void add();
}
public Interface AImpl implements CoreA{
void add(){};
}
public class B{
public void demo(){
a …Run Code Online (Sandbox Code Playgroud) 我了解到java 对象头包含hashcode,gc year,biased lock等信息.然后一个谜题来到我面前,为了明确表达我的问题.我举个例子.
这是代码:
public class Demo{
@Override
public int hashCode(){
System.out.println("the hashCode method was called");
return super.hashCode();
}
public static void main(String[] args){
Demo demo = new Demo();
System.out.println("after generate an object");
//
Set<Demo> set = new HashSet<Demo>();
set.add(demo);
}
}
Run Code Online (Sandbox Code Playgroud)
并输出:
after generate an object
the hashCode method was called
Run Code Online (Sandbox Code Playgroud)
我想当我们新建一个对象时,jvm会在对象头中设置hashcode.但是如果为了生成hashCode,它应该调用这个对象的hashCode方法. 但是根据输出似乎它没有在新的对象时调用hashCode方法.并将值添加到hashSet 中调用hashCode方法,这是预期的.
所以我的问题是:jvm什么时候在对象头中分配哈希码值?它发生在一个新的对象阶段?
我想row_number基于如何从基于表的多个列获取唯一记录的
功能中
删除重复项。但是被语法错误阻止了。我的用例如下:

和我的SQL如下:
select demo.*,
row_number() over (partition by id order by creator desc) as rn
from demo
Run Code Online (Sandbox Code Playgroud)
但它告诉我:
在“(”附近:语法错误:
我不知道发生了什么,我进行了一些搜索,例如如何在sqlite中使用ROW_NUMBER 。不幸的是,我仍然不知道自己犯了什么错误。一切都会感激的。
我知道有两种方法可以构建一个数组:
int[] a = {1,2,3};
int[] b = new int[]{1,2,3};
Run Code Online (Sandbox Code Playgroud)
到现在一切都好.
但是当使用for循环来处理数组的数据时.出现了一些东西.
至于这种方式:
for(int data:a){}
Run Code Online (Sandbox Code Playgroud)
一切都很好,
但就下面的方式而言,出现了惊喜:
for(int data:{1,2,3}){}
Run Code Online (Sandbox Code Playgroud)
它无法通过编译.和错误信息是:
此行有多个标记
- 语法错误,插入"}"以完成ArrayInitializer
- 语法错误,插入";;"语句"完成ForStatement
- 类型不匹配:无法从int []转换为int
- 令牌":"上的语法错误,=预期
我想找出原因,而且我想知道当没有new关键字时数组是在堆栈还是堆内存中?并且我知道当使用新关键字时它存在堆内存.