我对此进行了研究,并没有发现简单的(直截了当的解释)。
如果有人能(简单地)解释我如何使用 ArrayList 作为构造函数的参数,我将不胜感激。
例如,(这不是我正在处理的实际代码,我尝试给出一个单独的示例,以便更好地理解)
public class Shopping
{
//Instance of the variables
private double money;
private ArrayList<String> items;
//Constructor
public Shopping(double price, ArrayList<String> Item??){
money = price;
//Unsure as to what to put here for the ArrayList
//Would it just be?
items = Item;
//Or would I put String Item as the parameter instead of the ArrayList....
}
}
Run Code Online (Sandbox Code Playgroud)
这个类中没有 main 方法。由于将进行单元测试。将有一种方法可以将商品添加到购物清单中。
我很抱歉我之前的解释不是很清楚。但基本上,如果我在构造函数中有 ArrayList 作为参数..
我将如何在测试用例中调用构造函数?
我有一个存储名为 Movie 的对象的数组列表。对象包含变量,如名称、发布日期、流派等。有没有办法复制数组,这样我就可以将它一个一个排序,保持原始数据不变。我将在文本区域中显示数据。
我目前对 Map 何时优于 HashMap 或其他方式感到困惑,对于列表/数组列表也是如此...
有人可以请 ELI5 吗?我知道如何使用它们,但我需要有人在应该使用它们时为我清理它,谢谢。
我在创建Arraylist一个新类时遇到了问题(我使用的是DrJava IDE)。任务是创建一个带有n编号为 的彩票的构造函数1 to n。
import java.util.ArrayList;
public class Tombola {
private ArrayList<Integer> arr;
public Tombola(int n){
for (int i = 0; i < n-1; i++){
this.arr.add(i) = i + 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
意想不到的类型。
要求:变量。
发现:值。
我试图改变n和i整数,但它没有帮助。
我正在尝试编写一个程序来执行 的深拷贝List<List<Integer>>,我正在这样做:
public static List<List<Integer>> clone(final List<List<Integer>> src)
{
List<List<Integer>> dest = new ArrayList<List<Integer>>();
for( List<Integer> sublist : src) {
List<Integer> temp = new ArrayList<Integer>();
for(Integer val: sublist) {
temp.add(val);
}
dest.add(temp);
}
return dest ;
}
Run Code Online (Sandbox Code Playgroud)
这是一个好方法吗?是否有可能摆脱内循环?事实上,每个内部子列表都可以增长到很大的长度。
假设我创建一个对象如下:
formSummery: any = [];
{
"jobs" : [ {
"job_id" : 10,
"users" : [ {
"user_id" : 11,
"data_points" : [ {
"efficiency" : "good",
"form_id" : "2",
"instrument_type" : "plug",
"background_value" : "7",
"surveyed_item_id" : "100",
"duration" : 0,
"start_time" : "2017-07-20T04:04:43.000Z",
"datastream" : "GENERIC",
"username" : "abc@gmail.com.com"
}]
} ]
} ]
}
Run Code Online (Sandbox Code Playgroud)
删除属性数据流、用户名和 start_time 以结束新的 formSummery 的最佳方法是什么?
{
"jobs" : [ {
"job_id" : 10,
"users" : [ {
"user_id" : 11,
"data_points" : [ …Run Code Online (Sandbox Code Playgroud) 我正在尝试搜索数组中的一个元素。当得到它时,我需要在数组末尾附加一些元素。
我尝试与此类似。
List dataModelo = allMakers //THIS IS THE MAIN ARRRAY
.where((modelo) =>
modelo["fabricante"] ==
fabricante["fabricante"])
.toList()
.addAll([
{
"id": 0,
"fabricante":
'Test,
"modelo":
'Test'
}
]);
Run Code Online (Sandbox Code Playgroud)
但是返回
此处的表达式具有“void”类型,因此不能使用。
所以有人知道我该怎么做?
解决方案:
dataModelo = allMakers
.where((modelo) =>
modelo["fabricante"] ==
fabricante["fabricante"])
.followedBy([
{
"id": 0,
"fabricante":
'TEXT',
"modelo":
'TEXT'
}
]).toList();
Run Code Online (Sandbox Code Playgroud) 我正在尝试将对象添加到我的 ArrayList CoupleList 中,但我不断收到错误消息。
我的 WordCouple 类的构造函数
public class WordCouple {
private String first;
private String second;
public WordCouple (String first, String second) {
first = this.first;
second = this.second;
}
Run Code Online (Sandbox Code Playgroud)
我的 WordCoupleList 类的构造函数
import java.util.ArrayList;
public class WordCoupleList {
private ArrayList<WordCouple> coupleList;
public WordCoupleList(String[] words) {
for(int i = 0; i < words.length -1; i ++) {
for(int j = i+1; j < words.length; j++) {
coupleList.add(new WordCouple(words[i], words[i+j]));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是WordCouple从 a 中创建所有可能的实例String …
相对较新到Java 8,我想知道为什么它允许第一个变体(merge功能不是必要的)Collectors.toMap()一起工作时List:
static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Run Code Online (Sandbox Code Playgroud)
AList允许完全重复的值。想象一个用例,其中开发人员用于stream转换List为MapJava 8 将 RUNTIME 公开为:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key ...
Run Code Online (Sandbox Code Playgroud)
不应该要求在编译时捕获这种情况吗?AFAIK,哈希图用于在放置Entry重复项时简单地替换旧的key。如果开发人员确定数据中存在重复值并希望将此异常作为警告处理,他会不会首先使用Set代替List?例如。:-
public class Employee{
public String name;
public String surname;
public Employee(String firstname, String secondName) {
this.name=firstname;this.surname=secondName;
}
public boolean equals(Object o){
return (o instanceof Employee) && ((Employee)o).name.equals(this.name) …Run Code Online (Sandbox Code Playgroud) 我有一个里面有 2 个对象的数组列表,我需要对它们中的最后一个进行多次重复 N 次。例如。
Arraylist = [Z0123,Z0321]
Run Code Online (Sandbox Code Playgroud)
我需要这个结果
ArrayList = [Z0123,Z0321,Z0321,Z0321,N...]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用最后一个元素,但这不适用于 N 个元素
for(int i = 0; i < statusCorrigido.size(); i++) {
int tamanho = statusCorrigido.size()-1 ;
listaFinal.add(statusCorrigido.get(i));
listaFinal.add(statusCorrigido.get(tamanho));
}
Run Code Online (Sandbox Code Playgroud) arraylist ×10
java ×8
arrays ×2
hashmap ×2
list ×2
clone ×1
collectors ×1
constructor ×1
deep-copy ×1
dictionary ×1
drjava ×1
duplication ×1
flutter ×1
java-8 ×1
string ×1
typescript ×1