我正在尝试在 Java 上做一个应用程序,我需要在文件中保存 ArrayLists。我正在寻找如何做到这一点,我找到了下一个代码:
ArrayList<Expediente> al = new ArrayList<Expediente>();
Expediente exp = new Expediente("Expediente data");
try{
File file = new File("Pathname");
FileOutputStream ofs = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(ofs);
oos.writeObject(al);
oos.close();
ofs.close();
} catch (IOException e) {
System.err.println("Error!");
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是,如果我想保存相同的 ArrayList 但使用另一个数据,或者如果我重新打开文件,文件中的第一个数据将被覆盖。
有人能告诉如何将这些新数据附加到文件中吗?
谢谢你。
任何人都可以详细说明 IList 和 ICollection。而且,我们何时何地使用 IList 或 ICollection
让我们假设我调用第三方 API 并返回一个可变的 N 多对象列表。该列表可以小到 10 个对象,也可以大到几千个。然后我总是想在返回的列表的索引 0 处插入一个对象。我知道我可以轻松地在索引 0 处调用 add ,但这将是 O(n),因为它会为插入移动每个对象。我的问题是,使用我计划在开始时插入的项目创建一个新列表,然后在返回的第 3 方 N 多列表中调用该新列表上的 addAll 平均会更快(处理明智)吗?
我有以下字符串(str):
String str="${name}/${family}/${child}/${fname}"
Run Code Online (Sandbox Code Playgroud)
我在java中使用什么正则表达式模式来获取“${”和“}”之间的所有子字符串并将它们保存在一个arraylist中,因此结果将是一个具有以下值的数组列表:“name”、“family”、“孩子”,“名字”
import java.util.ArrayList;
public class list {
protected ArrayList<String> a = new ArrayList<String>();
public boolean ad(String aa)
{
boolean t=true;
a.add(aa);
for(String value : courses)
{
if(a.contains(value))
{
a=false;
}
else
{
a=true;
}
}
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
如果 arraylist 课程包含重复元素,该程序应该返回 false。否则,如果我们正在插入新元素,则返回 true。
上述代码的预期输出是
true
Run Code Online (Sandbox Code Playgroud)
但它只在任何条件下返回 false。
所以我试图创建这个循环,它可以从数组列表中删除给定字符串中的特定字符。问题是当一行中有两个相同的字符时,它会错过第二个,因为当第一个被删除时,第二个被推到数组列表中的一个位置。这是我到目前为止。有人知道怎么修这个东西吗?我仍然是 Java 的初学者,所以任何帮助将不胜感激。
ArrayList<Character> list = new ArrayList();
String string = "BuBBuopB";
for(int i=0;i<string.length();i++)
list.add(string.charAt(i));
System.out.println("BEFORE: " + list);
for(int i=0;i<list.size();i++)
if(list.get(i).equals('B'))
list.remove(i);
System.out.println("AFTER: " + list);
Run Code Online (Sandbox Code Playgroud)
从这个例子打印的是 AFTER: [u, B, u, o, p] 当我希望它是 AFTER: [u, u, o, p]
我有MyTrips fragment其中包含ViewPager. 所以它包含两个选项卡,即两个片段,一个是ScheduleListFrgment,另一个是TripHistoryFragment。
现在,当我MyTrips fragment首先替换它显示ScheduleListFragment,从服务器加载数据并显示在列表中时,它还从服务器加载 tripHistory 的数据。
所以现在我已经给了列表项的 click 事件,ScheduleListFragment所以onClick它替换ScheduleListFragment为PostDetailsFragment.
现在,在我按下后,PostDetailsFragment它显示为空ScheduleListFragment,没有列表。
我想在用户从 from 按下时显示列表PostDetailsFragment。
ScheduleListFragment
public class ScheduledTravelFragment extends Fragment implements GetScheduledTripsAsyncTask.GetScheduleTripsCallBack{
public RecyclerView mRecyclerView_list;
private ArrayList<Trips> mTripsList;
private ScheduledTravelAdapter mTripsAdapter;
public TextView mTextViewData;
private String mDateTime;
private SessionData sessionData;
private RelativeLayout parentLayout;
public ScheduledTravelFragment() {
// Required empty public constructor
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我有一个静态的 ArrayList.Each 线程运行以对 ArrayList 进行洗牌,然后我得到了错误的结果。
public void collectoinTest() {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
public void run() {
for (int j = 0; j < 10000; j++) {
Collections.shuffle(list);
System.out.println( list);
}
}
}).start();
}
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
[8, 9, 6, 5, 1, 7, 3, 4, 6, 0]
Run Code Online (Sandbox Code Playgroud)
它有重复的元素,谁能解释一下?
List<Map<String, String>> personList = new ArrayList<HashMap<String, String>> ();
String tempcnic = SBox.getText().toString();
HashMap<String,String> temp = new HashMap<>();
for (int i = 0; i < personlistcopy.size(); i++) {
temp = personlistcopy.get(i);
if (temp.get(TAG_CNIC) == tempcnic) {
Toast.makeText(History.this, temp.get(TAG_CNIC), Toast.LENGTH_SHORT).show();
personList2.add(temp);
}
temp = null;
}
Run Code Online (Sandbox Code Playgroud)
我有一个ArrayList包含HashMaps,
HashMap包含 4 个具有相应值的键。
请帮我做“搜索并找到给定 cnic 中的所有HashMapsArrayList并将它们添加到新的ArrayList。新的ArrayList将拥有该 cnic 的所有记录。”
我正在使用此方法从属性文件中读取:
public void loadConfigFromFile(String path) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(path);
prop.load(input);
/*saved like this:*/ //emails: abc@test.com, bbc@aab.com, ..
String wordsS = prop.getProperty("keywords");
String emailS = prop.getProperty("emails");
String feedS = prop.getProperty("feeds");
emails = Arrays.asList(emailS.split(",")); //ERROR !!
words = Arrays.asList( wordsS.split(","))); //ERROR !!
feeds = Arrays.asList( feedS.split(",")); //ERROR !!
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud)