有没有办法构建一个List<string>包含单个字符串的类型列表N而不使用循环?类似的东西,String(char c, int count)但代替字符串列表.
List<string> list = new List<string>() { "str", "str", "str", ..... N times };
Run Code Online (Sandbox Code Playgroud) 如何用 ArrayList 替换以下数组。
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
Run Code Online (Sandbox Code Playgroud) 我有这个:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class ListTest {
public static void main(String[] args) {
String[] values = { "yes", "no"};
List<String> aa = Arrays.asList(values);
System.out.println(aa.getClass().getName());
aa.remove(0);
}
}
Run Code Online (Sandbox Code Playgroud)
它给:
$ java ListTest
java.util.Arrays$ArrayList
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at ListTest.main(ListTest.java:12)
Run Code Online (Sandbox Code Playgroud)
问题:我理解为什么我会得到这个例外.这是因为正在使用ArrayList内部的类Arrays.java没有remove()方法.我的问题是,如果有人(任何用户,像我一样)在使用之前知道List他们收到的是什么,它不包含remove方法?
我有以下代码片段:
public void doSomething() {
float array[] = new float[2];
array[0] = (float) 0.0;
array[1] = (float) 1.2;
someMethod(array);
}
public void someMethod(Object value) {
//need to convert value to List<Float>
}
Run Code Online (Sandbox Code Playgroud)
正如您在上面所看到的,我想将值变量(即数组(但作为Object传递))转换为List.我尝试了以下建议:从数组创建ArrayList
new ArrayList<Float> (Arrays.asList(value));
Run Code Online (Sandbox Code Playgroud)
但是,它没有编译.
任何提示?
首先,我想说我知道之前曾在以下地点(其中包括)询问过这个问题,但我的答案并没有取得任何成功:
我想要做的是以下内容:
double[] FFTMagnitudeArray = processAudio.processFFT(audioData);
List<Double> FFTMagnitudeList = Arrays.asList(FFTMagnitudeArray);
audioData.setProperty("FFTMagnitudeList", FFTMagnitudeList);
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
"Type mismatch: cannot convert from List<double[]> to List<Double>"
Run Code Online (Sandbox Code Playgroud)
这对我来说没有意义,因为我认为List是必要的,而Array.asList(double [])将返回Double的列表,而不是double [].我也试过以下,但无济于事:
List<Double> FFTMagnitudeList = new ArrayList<Double>();
FFTMagnitudeList.addAll(Arrays.asList(FFTMagnitudeArray));
List<Double> FFTMagnitudeList = new ArrayList<Double>(Arrays.asList(FFTMagnitudeArray));
Run Code Online (Sandbox Code Playgroud)
我一直得到同样的错误.
那么我该如何创建List呢?
我希望在Java中有一个可调整大小的结构来保持一个维度数组或类型为double的向量.什么是最好的方法?
是否可以将数组作为参数传递给Arraylist?可能是一个Vector?
如果不是什么是非常合理的解决方案?
毕竟,基于提出的实现,从结构中获取值并在计算中使用它们的最有效方法是什么(添加数组,乘法数组等).
我有一个Java项目,它会查询一个返回String类似的API :
"['test1', 'test2', 'test3']" // the array is a string
Run Code Online (Sandbox Code Playgroud)
所以基本上是一个解析为a的数组对象String.我想知道将其转换为真正的Java的最佳方法List.
这可能是重复的,但我看不出有任何关于此错误的问题,所以请道歉.
我正在尝试使用该remove()方法从我的移除整数ArrayList,但它给了我java.lang.UnsupportedOperationException.remove方法应该int对我的理解采用一个或整数,或者来自的值ArrayList,但这些似乎不起作用并给出相同的错误.
我也尝试使用"深度"作为index,因为这是index我想删除的.
这是我的代码:
import java.util.*;
public class EP{
public static List<Integer> items = Arrays.asList(12, 13, 48, 42, 38, 2827, 827, 828, 420);
public static void main(String[]args){
System.out.println("Exam List");
for(Integer i: items){
System.out.println(i);
}
Scanner scan = new Scanner(System.in);
System.out.println("Enter depth");
int depth = scan.nextInt();
System.out.println("Enter value");
int value = scan.nextInt();
System.out.println(mark(depth, value));
}
public static int mark(int depth, int value){
int ret = -1; //This …Run Code Online (Sandbox Code Playgroud) 是否有一种简单/优雅的方法可以在列表中为地图中的每个键求和数字,例如Map<String, List<BigDecimal>>我希望得到的Map<String, BigDecimal>?我找不到/想出来......