我有一个程序可以处理来自传感器系统的大量传感器数据.我目前正在考虑将程序的输出写入文本文件,以便我可以从程序中检查它是否正常处理.
现在我在ArrayList之前编写一些标识符,然后使用将ArrayList写入文件ArrayList.toString().
lineToWrite = identifer1 + ";" + identifier2 + ";" + ArrayList.toString()
Run Code Online (Sandbox Code Playgroud)
输出文件总共包含21行,并且ArrayLists从100个项目到400.000个项目大.使用该toString()方法使我通常无法使用任何文件编辑程序打开文件并检查它们.
我想过对ArrayList中的项进行一些小处理:
String lineToWrite = "";
String arrayListString = "\n";
for(String s : sensorLine){
arrayListString += "\t" + s + "\n";
}
lineToWrite = identifer1 + ";" + identifier2 + ";" + arrayListString;
Run Code Online (Sandbox Code Playgroud)
但似乎这对于一些足够大的ArrayLists来说是永远的.有没有人有更好/更快的方法来做这个或知道一个好的文件查看程序?
我使用了以下,没有以下问题:
作为传感器数据的一个侧面说明:我总共有230万个传感器输入.
为了扩展问题,我可能不得不补充说,它是将巨大的数组拆分成一个被证明是个问题的单个字符串的一部分.程序在数组上迭代非常缓慢,因为它只是增加了arrayListString每次传递的大小,并且这占用了大量的内存/处理能力.
至于写方法本身,我使用a BufferedWriter(),用实际方法变量的占位符:
output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, toAppend), "UTF-8"));
Run Code Online (Sandbox Code Playgroud)
对于我正在使用的实际写作:
output.append(line) …Run Code Online (Sandbox Code Playgroud) 我有这个数组:
var arrayExample = [
{productId: 1, quantity: 2, name: example, description: example},
{productId: 1, quantity: 2, name: example, description: example},
{productId: 1, quantity: 2, name: example, description: example},
{productId: 1, quantity: 2, name: example, description: example}];
Run Code Online (Sandbox Code Playgroud)
我的问题是
如何获取阵列的所有项目,但只接受每个对象的productId和数量?因此,拥有一个包含所有对象但只包含两个值的数组?数组的对象数是可变的
结果:
var arrayExampleNew = [
{productId: 1, quantity: 2},
{productId: 1, quantity: 2},
{productId: 1, quantity: 2},
{productId: 1, quantity: 2}];
Run Code Online (Sandbox Code Playgroud)
对不起我的英语不好
我必须创建一个程序,接受用户输入的数字,然后将它们添加到ArrayList,然后以多种方式操作数据.数字必须大于或等于0.我有一个问题,将用户输入添加到ArrayList,我的try和catch语句停止程序崩溃但我无法向ArrayList添加任何东西,有人可以告诉我是什么我在添加过程中做错了吗?这是我的代码:
import java.util.ArrayList;
import java.util.Collections;
public class SumElements extends javax.swing.JFrame {
ArrayList <Integer> values = new ArrayList();
Run Code Online (Sandbox Code Playgroud)
...
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
try
{
//clear outputArea
outputArea.setText(null);
valueInput.setText(null);
outputLabel.setText(null);
//declare variables
int value = Integer.parseInt(valueInput.getText());
//validate input
if (value >= 0){
//add item to array
values.add(value);
//display values
Collections.sort(values);
for (int i = 0; i < values.size(); i++)
{
outputArea.setText(outputArea.getText() + values.get(i) + "\n");
}
}
}
//set default
catch (NumberFormatException a)
{
outputLabel.setText("Please input a valid number.");
} …Run Code Online (Sandbox Code Playgroud) 如何在Java中将字符串转换为布尔ArrayList?
例如:我有这样的字符串数组:
String[] strs= {"true","false","false","true",..etc};
Run Code Online (Sandbox Code Playgroud)
现在,我想将所有上述值放入布尔ArrayList中.
List<Boolean> bools=[true, false, false, true, ..etc]
Run Code Online (Sandbox Code Playgroud)
我可以在下面做这样的事情,但我希望这个任务可以在一行代码中完成.
String[] strs={"true","false","false","true",..etc};
List<Boolean> bools=new ArrayList<Boolean>();
for(String x:strs)
bools.add(Boolean.parseBoolean(x));
Run Code Online (Sandbox Code Playgroud) 我有以下Javascript对象数组,如果对所有对象都为true,我需要检查输出属性,如果对所有对象输出true则返回true否则返回false,任何人都可以帮我实现吗?
var array=[{"id":100,"output":true},{"id":200,"output":true}]
Run Code Online (Sandbox Code Playgroud)
更新
我已经尝试了这个代码但是如果只有一个输出为真而不是所有输出它会执行打印
function check(){
var data=[{"id":100,"output":false},{"id":200,"output":true}]
data.every(function (e) {
if(e.checked===true){
console.log("print something")
}
});
}
Run Code Online (Sandbox Code Playgroud)
代码有什么问题?
Arraylist.remove删除第一次出现而不是索引,即使我输入了一个int .remove(int i)?让我有一个随机数字的arraylist"数字",这个例子让我们说{1,3,5,2,4,6}
ArrayList<Integer> numbers = new ArrayList<Integer>();
aList.add(1);
aList.add(3);
aList.add(5);
aList.add(2);
aList.add(4);
aList.add(6);
Run Code Online (Sandbox Code Playgroud)
我想根据他们的索引删除所有奇数.我创建了另一个包含索引的Arraylist:
int origSize = numbers.size();
for (int i = 0; i < origSize; i++) {
if (numbers.get(i) % 2 != 0) {
numbers.add(numbers.get(i));
remover.add(i);
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我使用.remove函数根据索引删除元素时,它会删除第一个匹配项.我的语法是正确的,或者我正在做.remove(int i):
for (int i = (remover.size() - 1); i>=0; i--) {
numbers.remove(remover.get(i));
}
Run Code Online (Sandbox Code Playgroud)
在我的例子中,5发生在索引2.但是运行.remove(remover.get(i)),应该.remove(2)删除第一次出现的2 ...最终,我得到{3,5,4,6},因为它删除了2,1和0而不是那些索引.为什么会这样?
非常感谢你的帮助.
我有一个recyclerview,我显示一个字符串,其中包含一个id,名称和联系人编号作为单个字符串.我想隐藏字符串中的id.我使用以下代码将字符串添加到数组列表.我在文本视图中显示连接的字符串,如(1-XYZ 0123456789)
private void showGuestDetails(String response) {
Guest_info guest_info = new Guest_info(response);
guest_info.ParseGuestInfo();
String guestid[] = guest_info.guest_id;
for (int i = 0; i < guestid.length; i++) {
list.add(guest_info.guest_id[i] + "-" + guest_info.guest_name[i] + " " + guest_info.guest_mobile[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的适配器类: -
public class GuestAdapter extends RecyclerView.Adapter<GuestAdapter.GuestViewHolder> {
private List<String> list_item ;
public Context mcontext;
private BookingActivity bookingActivity;
public GuestAdapter(List<String> list, Context context, BookingActivity bookingActivity) {
list_item = list;
mcontext = context;
this.bookingActivity =bookingActivity;
}
// Called when RecyclerView needs a …Run Code Online (Sandbox Code Playgroud) 今天我需要一些帮助,将4个用户定义的字符串,2个int和4个双变量作为一个索引添加到arraylist中.
例如:(我会为你减少一些变量,并记住sc是我的扫描仪对象,测试结果超过100)
System.out.print("enter your name")
String name = sc.next();
System.out.print("enter your test results")
double results = sc.nextDouble();
System.out.print("enter your mobile number")
int mobile_num = sc.nextInt(); //I may change this to a string
Run Code Online (Sandbox Code Playgroud)
现在,从这里开始,我想捕获这些输入并将它们存储在数字1或用户名下的数组中.我还想知道如何使用这个相同的数组通过上面提到的代码存储来自用户的无限输入.
提前致谢!
如果我有一个arraylist,我想要一个排序和未分类的版本,我怎么能实现它.
试过收藏
ArrayList<Integer> someNumbers = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
如果我然后创建一个新的arraylist = someNumbers并对其进行排序.原始的一个也被排序了?
ArrayList<Integer> sortedNumbers = someNumbers;
Collections.sort(sortedNumbers);
Run Code Online (Sandbox Code Playgroud)
两个列表都被排序.
我怎样才能简单地实现我想做的事情?(获取一个已排序的副本,并保持其原始顺序的原始)
我正在获取json数据:
[
{
"name": "A",
"count": "2"
},
{
"name": "B",
"count": "1"
},
{
"name": "A",
"count": "3"
},
{
"name": "C",
"count": "10"
},
{
"name": "B",
"count": "2"
}
]
Run Code Online (Sandbox Code Playgroud)
所以我使用for循环,正常的东西迭代数据.但我想最终得到arraylist这样的结论:
Ç - > 7
A - > 5
乙 - > 3
请注意,list从最高到最低排序.到目前为止我尝试的是:
//create my arraylists
//has both name and count
ArrayList<String>namesWithCountList = new ArrayList<>();
//has only names
ArrayList<String>namesList = new ArrayList<>();
//get json array..
//iterate using for …Run Code Online (Sandbox Code Playgroud) arraylist ×10
java ×8
arrays ×4
collections ×2
javascript ×2
sorting ×2
android ×1
boolean ×1
filewriter ×1
for-loop ×1
if-statement ×1
large-data ×1
string ×1
textview ×1