java:25: '.class' expected error while merging arrays
Posted
by
user3677712
on Stack Overflow
See other posts from Stack Overflow
or by user3677712
Published on 2014-05-26T21:23:30Z
Indexed on
2014/05/26
21:24 UTC
Read the original article
Hit count: 113
Here is my code, it is asking me to call a class, I am confused as to do this. Noob to java, so any help would be greatly appreciated. line 25 is where the error occurs. This program is merging two arrays together into a new array.
public class Merge{
public static void main(String[] args){
int[] a = {1, 1, 4, 5, 7};
int[] b = {2, 4, 6, 8};
int[] mergedArray = merge(a, b);
for(int i = 0; i < mergedArray.length; i++){
System.out.print(mergedArray[i] + " ");
}
}
public static int[] merge(int[] a, int[] b){
// WRITE CODE HERE
int[] mergedArray = new int[a.length[] + b.length[]];
int i = 0, j = 0, k = 0;
while (i < a.length() && j < b.length()) //error occurs at this line
{
if (a[i] < b[j])
{
mergedArray[k] = a[i];
i++;
}
else
{
mergedArray[k] = b[j];
j++;
}
k++;
}
while (i < a.length())
{
mergedArray[k] = a[i];
i++;
k++;
}
while (j < b.length())
{
mergedArray[k] = b[j];
j++;
k++;
}
return mergedArray;
}
}
This program is merging two arrays together into a new array.
© Stack Overflow or respective owner