我有一个简单的数组,有点像这样
1 2 3 4 5 6 7 8 9
6 2 7 2 9 6 8 10 5
2 6 4 7 8 4 3 2 5
9 8 7 5 9 7 4 1 10
5 3 6 8 2 7 3 7 2
Run Code Online (Sandbox Code Playgroud)
所以,我们称之为matrix[5][9].我希望现在删除此矩阵中包含特定值的每一行,在本例中为10,所以我留下......
1 2 3 4 5 6 7 8 9
2 6 4 7 8 4 3 2 5
5 3 6 8 2 7 3 7 2
Run Code Online (Sandbox Code Playgroud) 我见过类似的问题而没有提供我正在寻找的答案,所以如果这被视为重复,我会提前道歉.我正在尝试将数组{1,2,3}和{4,5,6}组合成{1,2,3,4,5,6}.我做错了什么?我是java的新手.对不起,如果这个问题很愚蠢.
public class combine {
public static void main(String[]args){
int[]a = {1, 2, 3};
int[]b = {4, 5, 6};
int[]c = new int[a+b];
for(int i=0; i<a.length; i++)
System.out.print(c[i]+" ");
}
public static int[]merge(int[]a, int[]b){
int[]c = new int[a.length+b.length];
int i;
for(i=0; i<a.length; i++)
c[i] = a[i];
for(int j=0; j<b.length; j++)
c[i++]=b[j];
return c;
}
}
Run Code Online (Sandbox Code Playgroud) 使用Java 8我试图连接两个浮点数组:
void f(float[] first, float[] second) {
float[] both = ???
}
Run Code Online (Sandbox Code Playgroud)
通过快速SO搜索,我想我可以简单地按照这里的指示.所以我尝试过:
float both[] = FloatStream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();
Run Code Online (Sandbox Code Playgroud)
但是这不按照这里解释的那样编译.所以我尝试了效率较低的解决方案并Stream直接使用:
float[] both = Stream.concat(Arrays.stream(first), Arrays.stream(second)).toArray(float[]::new);
Run Code Online (Sandbox Code Playgroud)
它无法从我的日食编译说:
The method stream(T[]) in the type Arrays is not applicable for the arguments (float[])
Run Code Online (Sandbox Code Playgroud)
float[]在Java 8 中连接两个数组的最有效(和简单)方法是什么?
更新:显然问题的关键在于我必须处理float而不是处理double.
我的目标是实现一个方法,将任意数量的数组连接到它们的公共超类型的单个数组中,返回生成的(类型化的)数组.我有两个实现.
第一个(这个不需要简化):
public static <T> T[] concatArrays(Class<T> type, T[]... arrays) {
int totalLen = 0;
for (T[] arr: arrays) {
arr.getClass().getCom
totalLen += arr.length;
}
T[] all = (T[]) Array.newInstance(type, totalLen);
int copied = 0;
for (T[] arr: arrays) {
System.arraycopy(arr, 0, all, copied, arr.length);
copied += arr.length;
}
return all;
}
Run Code Online (Sandbox Code Playgroud)
让我们创建一些数组:
Long[] l = { 1L, 2L, 3L };
Integer[] i = { 4, 5, 6 };
Double[] d = { 7., 8., 9. };
Run Code Online (Sandbox Code Playgroud)
我们的方法调用:
Number[] n …Run Code Online (Sandbox Code Playgroud) 我的xml文件中有两个字符串数组.代码片段是
<string-array name="ECE1NAME">
<item>ENG1</item>
<item>MAT1</item>
<item>PHY1</item>
<item>CHM1</item>
<item>EG</item>
<item>FOC</item>
<item>CPL1</item>
<item>EPL</item>
</string-array>
<string-array name="ECE2NAME">
<item>ENG2</item>
<item>CHM2</item>
<item>PHY2</item>
<item>MAT2</item>
<item>ECED</item>
<item>BCM</item>
<item>PCL</item>
<item>CPL2</item>
<item>CDL</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)
现在我想根据我的类文件中的if条件将它组合成一个字符串.我的java代码片段是
if(messagec2.equals("1"))
{
sub=getResources().getStringArray(R.array.ECE1NAME);
}
if(messagec2.equals("2"))
{
sub=getResources().getStringArray(R.array.ECE1NAME);
sub=getResources().getStringArray(R.array.ECE2NAME);
}
Run Code Online (Sandbox Code Playgroud)
在第二个条件中,我想将ECE1NAME和ECE2NAME字符串数组放入相同的变量sub.Please帮助我.
我是Java的新手,试图拆分多个字符串并将其存储在String数组中.外行人计划如下:
Scanner sc = new Scanner(System.in);
String s1 = "Hello1 Hello2";
String s2 = "Hello3 Hello4";
String s3 = "Hello5 Hello6";
String[] parts = s1.split(" ");
parts = s2.split(" "); //Rewrites
parts = s3.split(" "); //Rewrites
for(String s4:parts) {
System.out.print(s4 + " ");
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出显然是:Hello5 Hello6.(如何在Java中拆分字符串)
无论我期望输出Hello1 Hello2 Hello3 Hello4 Hello5 Hello6.也就是说,传入的字符串不能替换数组中现有的字符串.
我想将两个字符串数组传递到一个字符串 varsargs 中。
IE。
public void doSomething(String... ){
}
public void test(){
String[] arrayOne = ...
String[] arrayTwo = ...
doSomething(arrayOne, arrayTwo); //Doesn't work but just for an example
}
Run Code Online (Sandbox Code Playgroud)
是连接两个数组的最佳方法还是有更好的方法?
我如何将2个阵列加在一起?
例如if:array 1 = [11,33,4] array 2 = [1,5,4]
然后得到的数组应为c = [11,33,4,1,5,4]; 任何帮助,将不胜感激
当我尝试在groovy中创建一个字节数组数组时,我收到一个错误.我的代码是:
def patch0 = [0, 2, 4, 8, 16] as byte[];
def patch1 = [0, 3, 6, 12, 24] as byte[];
def patches = [patch0, patch1] as byte[];
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
GroovyCastException: Cannot cast object '[B@7630e551' with class '[B' to class 'java.lang.Number'
Run Code Online (Sandbox Code Playgroud) 我想通过引用连接Java中的数组,就像这样.
int[] a = new int[] {1,2,3};
int[] b = new int[] {4,5,6};
int[] ab = concatArrayByReference(a,b); //Function to be defined
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做
a[2] = 90;
Run Code Online (Sandbox Code Playgroud)
AB应该等于{1,2,90,4,5,6}
这在Java中是否可行,那么通过引用拆分数组呢?
我希望这主要是出于性能原因,我想避免每次连接时都复制两个非常大的数组.
编辑:似乎在Java中不可能,那么我如何才能实现List的高性能实现呢?