我在收藏清单方面遇到了一些问题.我使用该列表来保存我从xml和textfiles收集的一些客户数据.
// First i create an instance of the list
List<Customer> cusList = new List<Customer>();
// Save files
String[] somefiles = Directory.GetFiles(//FromPath");
// Then i loop through some files and collect data for the list
for (int i=0; i<somefiles.length; i++)
{
if (some statements match)
{
// call a methode and save file data to the cuslist
cuslist= callmethode(somefiles);
}
else
{
System.Console.WriteLine("Do nothing");
}
}
Run Code Online (Sandbox Code Playgroud)
我想扩展所有文件的列表,但目前我只得到循环数据来自最后一个文件.
我如何处理它,它保存所有文件中的所有数据?
亲切的问候
首先!我刚接触编程,在博客中我看到了这两个定义.但我很困惑.它说
Class是对象的实例
而另一个说
类是对象的集合
哪个定义是正确的?怎么样?如果两个定义都是真的吗?怎么样?谢谢
我试过类似的东西
Thread
.getAllStackTraces()
.keySet()
.stream()
.map(
$->{
System.out.println
($.getStackTrace());
return $;
});
Run Code Online (Sandbox Code Playgroud)
我觉得这不是最好的方法.还有其他方法吗? 最简单的方法是什么?使用Stream API.(需要打印整个堆栈跟踪)
我有以下代码抛出ConcurrentModificationException,因为我在同一个列表中使用两个不同的迭代器,其中一个正在修改列表.因此,第二个迭代器在读取列表时抛出异常,因为其他迭代器已经修改了列表.
List<Integer> list = new ArrayList<>();
populate(list);//A method that adds integers to list
ListIterator<Integer> iterator1 = list.listIterator();
ListIterator<Integer> iterator2 = list.listIterator();
while (iterator1.hasNext()) {
if(iterator1.next() < 5)
iterator1.remove();
}
while (iterator2.hasNext()){
if(iterator2.next() < 5) {
//Call handler
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果它还没有到达一个尚未被删除的元素,那么内部如何iterator2
知道has已被其他迭代器修改过?怎么弄清楚其他一些人已经改变了?一种方法可能是跟踪大小,但这不是原因,因为其他迭代器可以只替换任何元素.list
iterator1
iterator
list
java collections exception concurrentmodification listiterator
我有一个程序试图创建一个典型的汽水箱程序,用户可以从列表中选择并添加到24的数组,以代表苏打水箱.但我希望它打印出这里的对象列表,以检查我的代码是否在我继续之前没有被破坏.除了我无法打印出数据.我用foor循环尝试了这个但它没有用
public class BeverageData
{
public string Name { set; get; }
public string Type { set; get; }
public decimal Price { set; get; }
public int Size { get; set; }
}
class Beverages : IEnumerable<BeverageData>
{
public static void BeverageRun()
{
var crate = new List<BeverageData>();
//Add some items to it
var newBeverage= new BeverageData();
newBeverage.Name = "Fanta";
newBeverage.Price = 0.75M;
newBeverage.Type = "Soda";
crate.Add(newBeverage);
crate.Add(new BeverageData() { Name = "Pepsi", Price = 0.25M, Type = "Soda" …
Run Code Online (Sandbox Code Playgroud) 在collections类中有一个方法sort()用于排序集合元素,但我有一个疑问,内部使用哪种排序算法对元素进行排序.我用Google搜索但没有得到正确答案.请帮帮我 .
我正在学习Java Collection Framework,并获得了该框架的基础知识。
是否有任何可靠的/官方的Collection框架层次结构图便于参考?我在网上提到,许多图表已经过时,不能依赖搜索结果。
谁能告诉我我们是否有任何可靠的图来表示Collection Framework的层次结构(例如,直到Java 1.8)。
使用get方法获取值时, TreeMap将值打印为null,而它正常工作HashMap()
.请在下面找到示例代码并为此提供输入.
它适用于Hashmap,因为它使用equals()/hashcode()
方法,而TreeMap是SortedMap,它不使用equals方法来比较两个对象.相反,它使用比较器/可比较来比较对象,但在使用get方法获取对象时,它将null作为响应.请在此提供一些清晰度.
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
class Employees implements Comparable<Employees>, Comparator<Employees> {
public Employees(String name, int id) {
super();
this.name = name;
this.id = id;
}
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public …
Run Code Online (Sandbox Code Playgroud) 我对Scala中的Iterator有一个混淆,那就是它是一个集合或一个一个接一个地访问Collection元素的方法?所以,如果它不是Collection,那么我们如何使用Iterator作为Iterator("a","number","of","words")?参考 - https://www.tutorialspoint.com/scala/scala_iterators.htm
我正在尝试HashMap
使用LinkedHashMap
和排序输出TreeMap
.
当我用它TreeMap
来理清HashMap
它就像一个魅力.
Map<Integer, String> hMap = new HashMap<Integer, String>();
hMap.put(40, "d");
hMap.put(10, "a");
hMap.put(30, "c");
hMap.put(20, "b");
System.out.println(" ");
System.out.println("before");
for (Map.Entry m1 : hMap.entrySet()) {
System.out.print(m1.getKey() + " " + m1.getValue() + " ");
}
System.out.println("after");
Map<Integer, String> hTree = new TreeMap<Integer, String>(hMap);
for (Map.Entry m2 : hTree.entrySet()) {
System.out.print(m2.getKey() + " " + m2.getValue() + " ");
}
Run Code Online (Sandbox Code Playgroud)
输出:before
20 b 40 d 10 a 30 c …