如何ArrayList以1而不是0 开始索引?有没有办法直接在代码中执行此操作?
(注意,我要求ArrayList,对于普通数组,请参阅在c#中的任意起始索引上初始化数组)
我需要过滤一个ArrayList并删除找到的元素.作为一个相对较新的Java,我想知道最有效的方法是什么(重要的是因为它在移动设备上运行).目前我这样做:
// We display only top-level dealers (parentId=-10)
ArrayList<DealerProductCount> subDealers = new ArrayList<DealerProductCount>();
for (DealerProductCount dealer : wsResponse.Dealers) {
if (dealer.ParentId != -10) subDealers.add(dealer);
}
wsResponse.Dealers.removeAll(subDealers);
Run Code Online (Sandbox Code Playgroud)
可以没有临时对象吗?也许通过直接操作(删除)迭代列表的元素?
我必须读取一个dict.txt文件,其中包含一行for line并将这些字符串添加到arraylist中.
我试过这个:
public ArrayList<String> myDict = new ArrayList<String>();
InputStream is = (getResources().openRawResource(R.raw.dict));
BufferedReader r = new BufferedReader(new InputStreamReader(is));
try {
while (r.readLine() != null) {
myDict.add(r.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但有些不对劲......
假设我有一个ArrayList,其中填充了不同类型的对象......
ArrayList<Fruit> shelf = new ArrayList<Fruit>();
Apple apple = new Apple();
Orange orange = new Orange();
Pear pear = new Pear();
shelf.add(apple);
shelf.add(orange);
shelf.add(pear);
Run Code Online (Sandbox Code Playgroud)
我想知道是否shelf包含一个Orange对象.我试过了
shelf.contains(Orange.class)
Run Code Online (Sandbox Code Playgroud)
但这并不是真的.我的理解是contains利用equals方法进行对象比较,所以我不确定为什么会这样.
我意识到我可以简单地遍历ArrayList并单独检查对象的类型,但我很好奇为什么contains不按照我期望的方式行事.
我已经创建了一个我的问题的简短例子.我正在匿名创建一个对象列表并将它们添加到一个ArrayList.一旦项目进入,ArrayList我稍后回来并向列表中的每个对象添加更多信息.如果你不知道它的索引,有没有办法从列表中提取特定对象?
我只知道对象的'名字',但你不能做list.get(ObjectName)任何事情.建议的方法是什么?每次我想要检索一个特定对象时,我宁愿不必遍历整个列表.
public class TestCode{
public static void main (String args []) {
Cave cave = new Cave();
// Loop adds several Parties to the cave's party list
cave.parties.add(new Party("FirstParty")); // all anonymously added
cave.parties.add(new Party("SecondParty"));
cave.parties.add(new Party("ThirdParty"));
// How do I go about setting the 'index' value of SecondParty for example?
}
}
class Cave {
ArrayList<Party> parties = new ArrayList<Party>();
}
class Party extends CaveElement{
int index;
public Party(String n){
name = …Run Code Online (Sandbox Code Playgroud) 我将ArrayList转换为JSONArray.我该如何将其转换回来?
最终结果必须是ArrayList.先感谢您.
编辑:
这是我将ArrayList转换为JSONArray的方法:
String string_object= new Gson().toJson(MyArrayList<OBJECT>);
JSONArray myjsonarray = new JSONArray(string_object);
Run Code Online (Sandbox Code Playgroud) 我得到Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0以下代码的例外.但无法理解为什么.
public class App {
public static void main(String[] args) {
ArrayList<String> s = new ArrayList<>();
//Set index deliberately as 1 (not zero)
s.add(1,"Elephant");
System.out.println(s.size());
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使它工作,但我试图理解这些概念,所以我将声明改为下面,但也没有工作.
ArrayList<String> s = new ArrayList<>(10)
Run Code Online (Sandbox Code Playgroud) 我想将一个arraylist存储到磁盘,所以我使用gson将其转换为字符串
ArrayList<Animal> anim=new ArrayList<Animal>();
Cat c=new Cat();
Dog d=new Dog();
c.parentName="I am animal C";
c.subNameC="I am cat";
d.parentName="I am animal D";
d.subNameD="I am dog";
anim.add(c);
anim.add(d);
Gson gson=new Gson();
String json=gson.toJson(anim);
public class Animal {
public String parentName;
}
public class Cat extends Animal{
public String subNameC;
}
public class Dog extends Animal{
public String subNameD;
}
Run Code Online (Sandbox Code Playgroud)
输出字符串:
[{"subNameC":"I am cat","parentName":"I am animal C"},{"subNameD":"I am dog","parentName":"I am animal D"}]
Run Code Online (Sandbox Code Playgroud)
现在我想用这个字符串转换回arraylist
我知道我应该使用类似的东西:
ArrayList<Animal> anim = gson.fromJson(json, ArrayList<Animal>.class);
Run Code Online (Sandbox Code Playgroud)
但这是不正确的,正确的语法是什么?
我需要添加一个Activity我可以列出的元素,ArrayList<CustomClass>我已经看到有一种更新,更好的方式来显示列表 - RecyclerView.
我的问题是如何将此实现到我的应用程序中.我发现我需要使用Adapter,但我不太明白如何正确实现整个过程.
如果你想知道,我指的是这个的文档的例子,这是我一直在读.
编辑:
更新了我的代码后,它说它无法解析符号setOnEntryClickListener:
public class voting extends Activity {
RecyclerView myList;
private ArrayList<Player> players; // Players
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voting);
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
players = (ArrayList<Player>)bundle.getSerializable("PLAYERS");
myList = (RecyclerView) findViewById(R.id.charactersList);
myList.setLayoutManager(new LinearLayoutManager(this));
CoursesAdapter adapter = new CoursesAdapter(players);
myList.setAdapter(adapter);
}
// OR RecyclerView with a click listener
CoursesAdapter clickAdapter = new CoursesAdapter(players);
clickAdapter.setOnEntryClickListener(new CoursesAdapter.OnEntryClickListener() {
@Override
public …Run Code Online (Sandbox Code Playgroud) 如何ArrayList在Kotlin中分配空值?
我想尝试将null值添加到我的自定义ArrayList
中
var mList = ArrayList<CustomClass>()
mList.add(null)
Run Code Online (Sandbox Code Playgroud)
在java中它可能但我怎样才能在Kotlin中实现这一点?我正进入(状态
null不能是非null类型kotlin的值
我需要插入null值以在RecyclerView中加载更多功能