你如何将文件的内容读入ArrayList<String>Java中?
这是文件内容:
cat
house
dog
.
.
.
Run Code Online (Sandbox Code Playgroud)
只需阅读每个单词ArrayList.
我正在从标准输入流中获取输入.如,
1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
要么
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
我正在使用:
std::string in;
std::getline(std::cin, in);
Run Code Online (Sandbox Code Playgroud)
但这只是抓住换行符,对吗?如何使用iosteam,string和cstdlib将它们分隔为换行符或空格?
我认为链接列表在添加元素时应该比arraylist更快?我刚刚测试了添加,排序和搜索元素所需的时间(arraylist vs linkedlist vs hashset).我只是使用java.util类进行arraylist和linkedlist ...使用每个类可用的add(object)方法.
arraylist out在填写列表时执行链表...并在列表的线性搜索中执行.
这是正确的吗?我在实施中做错了吗?
***************编辑*****************
我只是想确保我正确使用这些东西.这就是我正在做的事情:
public class LinkedListTest {
private List<String> Names;
public LinkedListTest(){
Names = new LinkedList<String>();
}
Run Code Online (Sandbox Code Playgroud)
然后我只使用链表列表方法,即"Names.add(strings)".当我测试arraylists时,它几乎相同:
public class ArrayListTest {
private List<String> Names;
public ArrayListTest(){
Names = new ArrayList<String>();
}
Run Code Online (Sandbox Code Playgroud)
我做得对吗?
试图写一个布尔方法,告诉某人是否是某人的后裔......但似乎无法做到这一点.当然,如果它是一个孩子......或者是孩子的后代,那么这个物体就是后代.
public boolean isDescendant(member x){
if (children.contains(x)){
return true;
}
else{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
但我在哪里或如何插入:
for (int i = 0; i < children.size(); i++){
isDescendant(children.get(i));
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
好的,所以我只是在了解内存泄漏.我跑valgrind找到内存泄漏.我得到以下内容:
==6134== 24 bytes in 3 blocks are definitely lost in loss record 4 of 4
==6134== at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==6134== by 0x8048B74: readInput(char&) (in calc)
Run Code Online (Sandbox Code Playgroud)
那么这肯定意味着泄漏是在我的readInput函数中吗?如果是这样,我如何摆脱内存泄漏?这是违规的功能:
double* readInput(char& command){
std::string in;
std::getline(std::cin, in);
if(!isNumber(in)){
if(in.length()>1){
command = 0;
}
else{
command = in.c_str()[0];
}
return NULL;
}
else{
return new double(atof(in.c_str()));
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我对cplusplus.com上的一些文档感到困惑.我是否将标准模板库与这两行代码中的任何一行一起使用?
for (std::string::const_iterator it = str.begin(); l < data.size; ++it, ++l)
Run Code Online (Sandbox Code Playgroud)
和
tile_tag(): distance(std::numeric_limits<int>::max()), dir(unknown_direction), used(false)
Run Code Online (Sandbox Code Playgroud)
请参阅:http: //www.cplusplus.com/reference/string/string/begin/ 和 http://www.cplusplus.com/reference/std/limits/numeric_limits/
如果我使用过STL,我如何修改它们以便在没有STL的情况下完成它们的工作?
谢谢!
编辑:我想这是我们对STL的定义:http://www.sgi.com/tech/stl/stl_index_cat.html
我没有看到const_iterator ......但我确实看到了最大值.但这是我用过的最大值吗?
这就是我所拥有的:
public class Node{
Object data;
Node next;
Node(Object data, Node next){
this.data = data;
this.next = next;
}
public Object getData(){
return data;
}
public void setData (Object data){
this.data = data;
}
public Node getNext(){
return next;
}
public void setNext(Node next){
this.next = next;
}
}
Run Code Online (Sandbox Code Playgroud)
如何编写代码以在列表末尾添加节点?
所以,如果我有
head -> [1] -> [2] -> null
Run Code Online (Sandbox Code Playgroud)
我怎么去
head -> [1] -> [2] -> [3] -> null
Run Code Online (Sandbox Code Playgroud)
实际上......我甚至不确定我是否必须添加到最后.我认为添加然后排序是有效的吗?不确定.
谢谢!
我试图不使用STL.我的代码中有这一行:
std::copy(buffer_, buffer_ + size_ + 1, new_buffer)
Run Code Online (Sandbox Code Playgroud)
如果我不想使用副本,这是正确的等价物吗?
for (int i = 0; i < (size_ + 1); i++){
new_buffer[i] = buffer[i];
}
Run Code Online (Sandbox Code Playgroud)
还是完全错了?还是一个人呢?或者是其他东西?
谢谢!
c++ ×4
java ×4
arraylist ×1
input ×1
linked-list ×1
memory-leaks ×1
optimization ×1
recursion ×1
stl ×1
valgrind ×1