有没有办法将目录中每个文本文件中的最大值保存到一个列表中?我有多个相同格式的文本文件,其中每个值在不同的行上:
(第一分)
(第二分)
(第三分)
我想将这三个整数中最大的整数保存到一个列表中,对于该文件夹中的所有其他文件也是如此。这可能吗?
答案 0 :(得分:0)
这是一种方法。
public class CurrentTimetables extends Fragment {
RecyclerView recyclerView;
static MusicRecyclerAdapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Timetables> list;
public CurrentTimetables(){
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.currenttimetable, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.timetablerecyclerView);
recyclerView.setHasFixedSize(true);
list = new ArrayList<Timetables>();
adapter = new MusicRecyclerAdapter(list, CurrentTimetables.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
tabBackground = new TabBackground(CurrentTimetables.this, list, spinnerItems, adapter, spinnerAdapter);
tabBackground.populateConditionsList();
inputs = new ArrayList<>();
populate();
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onStart() {
super.onStart();
}
public void populate(){
String dbURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
RequestQueue requestQueue = Volley.newRequestQueue(fragment.getActivity());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(dbURL, new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray jsonArray) {
if(!list.isEmpty()){
list.clear();
}
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int timetableID = Integer.parseInt(jsonObject.getString("timetableID"));
String timetableName = jsonObject.getString("timetableName");
String subjectName = jsonObject.getString("subjectName");
Timetables timetables = new Timetables(timetableID, timetableName, subjectName);
list.add(timetables);
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}
);
requestQueue.add(jsonArrayRequest);
}
}
您遍历目录中的所有文件,删除线条,将它们转换为int并从中获取最大值。