我正试图找出ArrayList通过其Id编号搜索客户的最佳方式.以下代码无效; 编译器告诉我,我错过了一个return声明.
Customer findCustomerByid(int id){
boolean exist=false;
if(this.customers.isEmpty()) {
return null;
}
for(int i=0;i<this.customers.size();i++) {
if(this.customers.get(i).getId() == id) {
exist=true;
break;
}
if(exist) {
return this.customers.get(id);
} else {
return this.customers.get(id);
}
}
}
//the customer class is something like that
public class Customer {
//attributes
int id;
int tel;
String fname;
String lname;
String resgistrationDate;
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在arraylist中获得对象的类型?
我需要制作一个IF语句如下(在C#中):
if(object is int)
//code
else
//code
Run Code Online (Sandbox Code Playgroud)
谢谢
根据Sun Java Implementation,在扩展期间,ArrayList增长到3/2它的初始容量,而对于HashMap,扩展速率是双倍.这背后的原因是什么?
根据实现,对于HashMap,容量应始终为2的幂.这可能是HashMap行为的原因.但在这种情况下,问题是,对于HashMap,为什么容量应始终为2的幂?
我有一个ArrayList重复条目的单词.
我想计算和保存数据结构中每个单词的出现次数.
我该怎么做?
我有两个问题,一点一点......首先,为什么我不能这样做:
List<Object> object = new List<Object>();
Run Code Online (Sandbox Code Playgroud)
第二,我有一个方法返回一个List<?>,我怎么把它变成一个List<Object>,我能够简单地抛出它吗?
谢谢!
也就是说,在不引用同一对象的情况下,我需要将一个列表的元素值复制到另一个列表中.这些是列表:
List<Integer> listA = new ArrayList<Integer>();
List<Integer> ListB = new ArrayList<Integer>();
listA = (added some values);
listB = (do what?)...
Run Code Online (Sandbox Code Playgroud)
PS.我为初学者的问题道歉,但我从未做过这样的事情.
我想动态地解析我的Json数组.并希望获得jsonarray下每个元素的KEYS数组.我通过迭代器得到这个.但没有按照输出json formate获得序列.
我的JSON Formate:
{
"result": "Success",
"AlertDetails": [
{
"ShipmentNumber": "SHP34",
"Customer": "BEST",
"DateCreated": "2012-08-29T04:59:18Z"
"CustomerName": "BEST"
},
{
"ShipmentNumber": "SHP22",
"Customer": "BEST",
"DateCreated": "2012-08-29T05:34:18Z"
"CustomerName": "Jelly"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
JSONArray array = jsonobject.getJSONArray("AlertDetails");
JSONObject keyarray = array.getJSONObject(0);
Iterator temp = keyarray.keys();
while (temp.hasNext()) {
String curentkey = (String) temp.next();
KEYS.add(curentkey);
}
Log.d("Parsing Json class", " ---- KEYS---- " + KEYS);
Run Code Online (Sandbox Code Playgroud)
我在logcate输出中获得的内容:
---- KEYS---- [DateCreated,CustomerName, Customer, ShipmentNumber]
Run Code Online (Sandbox Code Playgroud)
我想要的是 :
---- KEYS---- [ShipmentNumber, Customer, DateCreated,CustomerName]
Run Code Online (Sandbox Code Playgroud) 我有一个类型为patient_class的arraylist,并且arraylist类型用黄色加下划线,IDE提到了"新表达式中的冗余类型参数(使用菱形运算符代替)".
我的问题是:我应该使用钻石操作员吗?这是必须的吗?将记录存储到arraylist时,是否会出现任何数据丢失或任何其他问题?
这是我的arraylist:
public class Register_newpatient extends javax.swing.JFrame {
public Register_newpatient() {
initComponents();
groupbutton();
}
public void groupbutton()
{
ButtonGroup bg1=new ButtonGroup();
bg1.add(rbopd);
bg1.add(rbinpatientcare);
bg1.add(rbboth);
}
all_error_handling checkerror = new all_error_handling();
JFrame frame = new JFrame();
static ArrayList<patient_class>patientlist = new ArrayList<patient_class>();
Run Code Online (Sandbox Code Playgroud)
说实话,我不知道钻石操作员究竟是什么.
例如,如果你有
int a=2; int b=3;
ArrayList<Integer>integers=new ArrayList<Integer>();
integers.add(a); integers.add(b);
Run Code Online (Sandbox Code Playgroud)
和
String c="cow"; String d="deer";
ArrayList<String> strings= new ArrayList<String>();
strings.add(c); strings.add(d);
Run Code Online (Sandbox Code Playgroud)
他们会采取不同的记忆吗?任何帮助/答案都将受到高度赞赏,谢谢!
假设我们在集合中有一些项目,我们想要使用某个比较器对它们进行排序,期望列表中的结果:
Collection<Item> items = ...;
Comparator<Item> itemComparator = ...;
Run Code Online (Sandbox Code Playgroud)
其中一种方法是对列表中的项进行排序,例如:
List<Item> sortedItems = new ArrayList<>(items);
Collections.sort(sortedItems, itemComparator);
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用排序流:
List<Item> sortedItems = items
.stream()
.sorted(itemComparator)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我想知道,哪种方法更有效率?排序流是否有任何优点(如多核上的紧固排序)?
从运行时复杂性/速度的角度来看,效率很高.
我不相信自己能够实现一个完美的基准,学习SortedOps并没有真正启发我.