Java - Partially convert 2D string to 2D Integer ArrayList - java

I was trying to convert my 2D String into 2D Integer ArrayList, but I don't know how to. I have read some reference but didn't find anything related.
Here is my method:
public static ArrayList<int[]> convert2DStringTo2DIntArrayList(String[][] originalString, int lengthOfRow, int lengthOfColumn) {
ArrayList<int[]> targetList = new ArrayList<int[]>();
if (lengthOfRow == -1) {
lengthOfRow = originalString.length - 1;
}
if (lengthOfColumn == -1) {
lengthOfColumn = originalString[0].length - 1;
}
for (int i = 0; i <= lengthOfRow - 1; i++) {
for (int j = 0; j <= lengthOfColumn - 1; j++) {
//targetList.addAll(Integer.parseInt(Arrays.asList(originalString)));
}
}
return targetList;
}
When lengthOfRow and lengthOfColumn all equal to -1 this method will fully convert 2D String to 2D Integer ArrayList. No problem with String because the String array to be proceed is partially filled by integer. I met this problem is because my original methods are all written in basic types and string. On the mid way I found I cannot handle several problem with string array. By this reason I have to write several method to convert string to ArrayList.

This is the function that can help you.
public void ArrayList<ArrayList<Integer>> convert2DStringTo2DIntegerArray(String[][] original) {
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < original.length; i++)
{
ArrayList<Integer> tempArray = new ArrayList<Integer>();
for (int j = 0; j < original[i].length; j++)
{
tempArray.add(Integer.valueOf(original[i][j]));
}
list.add(tempArray);
}
return list;
}

Updated. To convert multidimensional array of integers represented as strings:
import java.util.List;
import java.util.ArrayList;
public class Convert
{
public static void main(String[] args)
{
List<List<Integer>> list = convert2DStringToList(new String[][]{ {"0", "1", "2"}, {"3", "4", "5"}, {"100", "200", "300", "500"}, {"22"} });
System.out.println(list.toString());
}
public static List<List<Integer>> convert2DStringToList(String[][] original)
{
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < original.length; i++)
{
List<Integer> subList = new ArrayList<>();
list.add(subList);
for (int j = 0; j < original[i].length; j++)
{
subList.add(Integer.valueOf(original[i][j]));
}
}
return list;
}
}
For given code the output is [[0, 1, 2], [3, 4, 5], [100, 200, 300, 500], [22]].

Related

Merge two arraylists into a new one and make it sort them (see example)

I have tried to solve this exercise for almost three hours now and I still don't understand what I am doing wrong. I am supposed to take two ArrayLists with numbers and merge them into one,but here's the catch, they must sorted like this:
If arraylist "A" has the numbers [1, 2, 3] and arraylist "B" has [9, 8, 7, 6, 5] then ArrayList "merge" should be [1, 9, 2, 8, 3, 7, 6, 5].
I.e it should alternate the numbers from the inputted arraylists A and B. If one arraylist is longer it should just keep on filling with numbers (like what happened to [7,6,5] in this case.
Also, we don't know the length of any of the arrayLists.
Here's one solution that I think should work well, but I can't get it to work.
All help is extremely appreciated!!
import java.util.ArrayList;
import java.util.Scanner;
public class test19 {
public static void main(String[] args) {
ArrayList<Integer> arrayListA = new ArrayList<>();
ArrayList<Integer> arrayListB = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("Write a number to place in ArrayList A, quit with '-1'");
int Local = sc.nextInt();
if(Local > 0) {
arrayListA.add(Local);
} else {
break;
}
}
System.out.println();
System.out.println();
while(true) {
System.out.println("Write a number to place in ArrayList B, quit with '-1'");
int Local = sc.nextInt();
if(Local > 0) {
arrayListB.add(Local);
} else {
break;
}
}
System.out.println(merge(arrayListB, arrayListA));
}
public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList<Integer> merge = new ArrayList<>();
if(a.size() < b.size()) {
//here we check which list is the smallest and use that one (so we don't try to add blankspaces from the longer list to the merge list)
for(int i = 0; i <= a.size(); i++) {
merge.add(a.get(i));
merge.add(b.get(i));
}
for(int j = a.size(); j <= b.size(); j++) {
merge.add(b.get(j)); //here we add the leftover numbers to the list
}
} else { //this means that list A is bigger than list B
for(int i = 0; i <= b.size(); i++) {
merge.add(a.get(i));
merge.add(b.get(i));
}
for(int j = b.size(); j <= a.size(); j++) {
merge.add(b.get(j));
}
}
return merge;
}
}
Here's your code fixed. Mainly it was a case of changing all the <= to just <. If you compare the differences you'll be able to see where you went wrong:-
import java.util.ArrayList;
import java.util.Arrays;
public class test19{
public static void main(String[]args){
ArrayList<Integer> arrayListA = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
ArrayList<Integer> arrayListB = new ArrayList<Integer>(Arrays.asList(11, 12, 13, 14, 15, 16, 17));
System.out.println(merge(arrayListA,arrayListB));
}
public static ArrayList<Integer> merge (ArrayList<Integer> a, ArrayList<Integer> b){
ArrayList<Integer> merge = new ArrayList<Integer>();
if (a.size()<b.size()) {
//here we check which list is the smallest and use that one (so we don't try to add blankspaces from the longer list to the merge list)
for(int i=0; i<a.size(); i++){
merge.add(a.get(i));
merge.add(b.get(i));
}
for(int j=a.size(); j<b.size(); j++){
merge.add(b.get(j)); //here we add the leftover numbers to the list
}
} else { //this means that list A is bigger than list B
for(int i=0; i<b.size(); i++){
merge.add(a.get(i));
merge.add(b.get(i));
}
for(int j=b.size(); j<a.size(); j++){
merge.add(a.get(j));
}
}
return merge;
}
}
Others have already answered this, just providing an alternate implementation that may be easier to read.
import java.util.*;
public class Main {
public static void main(String[] args)
{
ArrayList<Integer> arrayListA = new ArrayList<Integer>();
ArrayList<Integer> arrayListB = new ArrayList<Integer>();
arrayListA.add(1);
arrayListA.add(2);
arrayListA.add(3);
arrayListB.add(9);
arrayListB.add(8);
arrayListB.add(7);
arrayListB.add(6);
arrayListB.add(5);
merge(arrayListA, arrayListB);
}
public static void merge(ArrayList<Integer> arrayListA, ArrayList<Integer> arrayListB)
{
ArrayList<Integer> merged = new ArrayList<Integer>();
int maxSize = Math.max(arrayListA.size(), arrayListB.size());
for (int i = 0; i < maxSize; i++)
{
if(i < arrayListA.size())
merged.add(arrayListA.get(i));
if(i < arrayListB.size())
merged.add(arrayListB.get(i));
}
for(int i = 0; i < merged.size(); i++)
{
System.out.println(merged.get(i));
}
}
}
I have corrected your merge function, you just need to use < operator in for loops instead of <=
public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList<Integer> merge = new ArrayList<Integer>();
if(a.size() < b.size()) {
//here we check which list is the smallest and use that one (so we don't try to add blankspaces from the longer list to the merge list)
for(int i = 0; i < a.size(); i++) {
merge.add(a.get(i));
merge.add(b.get(i));
}
for(int j = a.size(); j < b.size(); j++) {
merge.add(b.get(j)); //here we add the leftover numbers to the list
}
} else { //this means that list A is bigger than list B
for(int i = 0; i < b.size(); i++) {
merge.add(a.get(i));
merge.add(b.get(i));
}
for(int j = b.size(); j < a.size(); j++) {
merge.add(a.get(j));
}
}
return merge;
}

Remove elements from 2D array

I want to remove elements that are in routedClients from array, so I converted it to an ArrayList, then used remove, finally I converted it back to double[][] array. But when I execute it, it gives me this message about this line:
double[][] remainingStockout = (double[][]) stockout.toArray();
The error is:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Double;
Any help would be really appreciated. :)
public double[][] removeSite(double[][] array) {
List<double[]> stockout = new ArrayList<double[]>(Arrays.asList(array));
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < routedClients.size(); j++) {
if (array[i][0] == routedClients.get(j)) {
stockout.remove(i);
}
}
}
double[][] remainingStockout = (double[][]) stockout.toArray();
return remainingStockout;
}
The below appears to work
double[][] remainingStockout = (double[][]) stockout.toArray(new double[][]{});
Full class for testing:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
static ArrayList<Double> routedClients = new ArrayList<Double>();
public static void main(String[] args) {
double[][] arr1 = { { 2, 4, 6 }, { 3, 6, 9 }, { 5, 10, 15 } };
routedClients.add(new Double(1));
routedClients.add(new Double(2));
routedClients.add(new Double(3));
print(arr1);
double[][] arr2 = removeSite(arr1);
print(arr2);
}
private static void print(double[][] arr1) {
for (int i = 0; i < arr1.length; i++) {
double[] arr2 = arr1[i];
for (int j = 0; j < arr2.length; j++) {
System.out.println("arr1[" + i + "][" + j + "] = " + arr1[i][j]);
}
}
}
public static double[][] removeSite(double[][] array) {
List<double[]> stockout = new ArrayList<double[]>(Arrays.asList(array));
System.out.println("length before = " + stockout.size());
for (int i = array.length-1; i >= 0; i--) {
for (int j = 0; j < routedClients.size(); j++) {
if (array[i][0] == routedClients.get(j)) {
System.out.println("removing " + routedClients.get(j));
stockout.remove(i);
}
}
}
double[][] remainingStockout = (double[][]) stockout.toArray(new double[][] {});
System.out.println("length after = " + remainingStockout.length);
return remainingStockout;
}
}
Here is the output
arr1[0][0] = 2.0
arr1[0][1] = 4.0
arr1[0][2] = 6.0
arr1[1][0] = 3.0
arr1[1][1] = 6.0
arr1[1][2] = 9.0
arr1[2][0] = 5.0
arr1[2][1] = 10.0
arr1[2][2] = 15.0
length before = 3
removing 3.0
removing 2.0
length after = 1
arr1[0][0] = 5.0
arr1[0][1] = 10.0
arr1[0][2] = 15.0
You are trying to cast an object into an array.
This cannot be done.
Instead you will need to convert each element in the array and then need to add it to they the 2D Array.
The following line will never work:
double[][] remainingStockout = (double[][]) stockout.toArray();
You can cast a double array into a list array because the compiler just makes a list with all the elements, but the compiler can't know which size to make a 2D array.
Maybe you could try creating a list of lists. You would need to traverse your array two more times though. One for assigning the values to the nested list and then to assign them again to the double array before returning the value.
you can use the overloaded method of toArray() .. like this -
double[][] remainingStockout = new double[array.length][array.length];
stockout.toArray(remainingStockout);

2-dimensional array with given number [duplicate]

Here is the code I have so far:
public static int mode(int[][] arr) {
ArrayList<Integer> list = new ArrayList<Integer>();
int temp = 0;
for(int i = 0; i < arr.length; i ++) {
for(int s = 0; s < arr.length; s ++) {
temp = arr[i][s];
I seem to be stuck at this point on how to get [i][s] into a single dimensional array. When I do a print(temp) all the elements of my 2D array print out one a time in order but cannot figure out how to get them into the 1D array. I am a novice :(
How to convert a 2D array into a 1D array?
The current 2D array I am working with is a 3x3. I am trying to find the mathematical mode of all the integers in the 2D array if that background is of any importance.
In Java 8 you can use object streams to map a matrix to vector.
Convert any-type & any-length object matrix to vector (array)
String[][] matrix = {
{"a", "b", "c"},
{"d", "e"},
{"f"},
{"g", "h", "i", "j"}
};
String[] array = Stream.of(matrix)
.flatMap(Stream::of)
.toArray(String[]::new);
If you are looking for int-specific way, I would go for:
int[][] matrix = {
{1, 5, 2, 3, 4},
{2, 4, 5, 2},
{1, 2, 3, 4, 5, 6},
{}
};
int[] array = Stream.of(matrix) //we start with a stream of objects Stream<int[]>
.flatMapToInt(IntStream::of) //we I'll map each int[] to IntStream
.toArray(); //we're now IntStream, just collect the ints to array.
You've almost got it right. Just a tiny change:
public static int mode(int[][] arr) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
// tiny change 1: proper dimensions
for (int j = 0; j < arr[i].length; j++) {
// tiny change 2: actually store the values
list.add(arr[i][j]);
}
}
// now you need to find a mode in the list.
// tiny change 3, if you definitely need an array
int[] vector = new int[list.size()];
for (int i = 0; i < vector.length; i++) {
vector[i] = list.get(i);
}
}
I'm not sure if you're trying to convert your 2D array into a 1D array (as your question states), or put the values from your 2D array into the ArrayList you have. I'll assume the first, but I'll quickly say all you'd need to do for the latter is call list.add(temp), although temp is actually unneeded in your current code.
If you're trying to have a 1D array, then the following code should suffice:
public static int mode(int[][] arr)
{
int[] oneDArray = new int[arr.length * arr.length];
for(int i = 0; i < arr.length; i ++)
{
for(int s = 0; s < arr.length; s ++)
{
oneDArray[(i * arr.length) + s] = arr[i][s];
}
}
}
change to:
for(int i = 0; i < arr.length; i ++) {
for(int s = 0; s < arr[i].length; s ++) {
temp = arr[i][s];
"How to convert a 2D array into a 1D array?"
String[][] my2Darr = .....(something)......
List<String> list = new ArrayList<>();
for(int i = 0; i < my2Darr.length; i++) {
list.addAll(Arrays.asList(my2Darr[i])); // java.util.Arrays
}
String[] my1Darr = new String[list.size()];
my1Darr = list.toArray(my1Darr);
I know its already been answered but here is my take. This function will take a 2d array input and return a 1d array output.
public int[] output(int[][] input){
int[] out = new int[input.length * input[0].length]
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input[i].length; j++) {
out[i + (j * input.length)] = input[i][j]
}
}
return out;
}
System.arraycopy should be faster than anything we can write. Also use the built-in java iterator on rows. Here is an example for double arrays. You should be able to use any type or class. If your rows are all the same length, then totalNumberElements = array2D.length * array2D[0].length;
static double[] doubleCopyToOneD(double[][] array2D, int totalNumberElements) {
double[] array1D = new double[totalNumberElements];
int pos = 0;
for (double[] row: array2D) {
System.arraycopy(row, 0, array1D, pos, row.length);
pos += row.length;
}
return array1D;
}
import java.util.*;
public class Main {
public static int A[][] = new int[3][3];
public static int B[] = new int[9];
public static void main(String[] args) {
int temo = 0,t;
Scanner s = new Scanner(System.in);
System.out.println("Enter No for Matrix A");
for (int row = 0; row < A.length; row++) {
for (int col = 0; col < A.length; col++) {
A[row][col] = s.nextInt();
}
System.out.print("\n");
}
for (int row = 0; row < A.length; row++) {
for (int col = 0; col < A.length; col++) {
t= A[row][col];
B[temo]= t;
temo++;
}
System.out.print("\n");
}
System.out.print("After Converted to one d \n");
for(int i =0;i<B.length;i++) {
System.out.print(" "+B[i]+" ");
}
}
}

Write a method that merges two array lists, alternating elements from both array lists

Write a method
public static ArrayList merge(ArrayList a, ArrayList b)
that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elemts from the longer array list. For example, if a is
1 4 9 16
and b is
9 7 4 9 11
then merge returns the array list
1 9 4 7 9 4 16 9 11
What I tried doing was writing a for loop with if statements such that a number is added to the merge array list from array list a when i is an even number (i%2==0) and from array list b when i is an odd number. I am however not sure how to deal with the fact that one array list can be longer than the other. Could anyone please help me out?
EDIT: Ok, here is the code (but it is far from correct):
public static ArrayList<Integer> merge(ArrayList<Integer> een, ArrayList<Integer> twee)
{
ArrayList<Integer> merged = new ArrayList<Integer>();
for(int i = 0; i<100; i++)
{
if(i%2!=0)
{
merged.add(a.get(i));
}
if(i%2 == 0)
{
merged.add(b.get(i));
}
}
System.out.println(merged);
return merged;
}
Iterators seem to do the trick most easily
public static <T> ArrayList<T> merge(Collection<T> a, Collection<T> b) {
Iterator<T> itA = a.iterator();
Iterator<T> itB = b.iterator();
ArrayList<T> result = new ArrayList<T>();
while (itA.hasNext() || itB.hasNext()) {
if (itA.hasNext()) result.add(itA.next());
if (itB.hasNext()) result.add(itB.next());
}
return result;
}
Without iterators:
public static <T> ArrayList<T> merge(List<T> a, List<T> b) {
ArrayList<T> result = new ArrayList<T>();
int size = Math.max(a.size(), b.size());
for (int i = 0; i < size; i++) {
if (i < a.size()) result.add(a.get(i));
if (i < b.size()) result.add(b.get(i));
}
return result;
}
Note, I've relaxed the method signature a bit. If you're implementing the merging using iterators, Collection (or even Iterable) will do. Otherwise, List will do. There is no need to require ArrayList as a method argument type
Without Iterator:
public static ArrayList merge(ArrayList a, ArrayList b) {
int c1 = 0, c2 = 0;
ArrayList<Integer> res = new ArrayList<Integer>();
while(c1 < a.size() || c2 < b.size()) {
if(c1 < a.size())
res.add((Integer) a.get(c1++));
if(c2 < b.size())
res.add((Integer) b.get(c2++));
}
return res;
}
Try this:I implemented using Array.
public static void main(String[] args) {
int[] first = { 1, 4, 9, 16 };
int[] second = { 9, 7, 4, 9, 11 };
int[] merge = new int[first.length + second.length];
int j = 0, k = 0, l = 0;
int max = Math.max(first.length, second.length);
for (int i = 0; i < max; i++) {
if (j < first.length)
merge[l++] = first[j++];
if (k < second.length)
merge[l++] = second[k++];
}
System.out.println(Arrays.toString(merge));
}
Output:
[1, 9, 4, 7, 9, 4, 16, 9, 11]
You don't need to check modulo, or you'll skip every second element from each input list.
public static <E> List<E> merge(List<E> een, List<E> twee) {
List<E> merged = new ArrayList<E>(een.size() + twee.size());
List<E> shorter = een.size() <= twee.size() ? een : twee;
List<E> longer = een.size() > twee.size() ? een : twee;
for (int i = 0; i < shorter.size(); i++) {
merged.add(een.get(i));
merged.add(twee.get(i));
}
for (int i = shorter.size(); i < longer.size(); i++) {
merged.add(longer.get(i));
}
return merged;
}
This generic version works for all kind of Lists and generic types.
Here is my solution
LinkedList<Integer> list3 = new LinkedList<Integer>();
Iterator<Integer> itA = list.iterator();
Iterator<Integer> itB = list2.iterator();
while(itA.hasNext() && itB.hasNext()){
list3.add(itA.next());
list3.add(itB.next());
}
I had the same situation and below was my solution:
// array list to hold the merged list
ArrayList<Integer> mergedList = new ArrayList<Integer>();
// Get the bigger size
int maxSize = listOne.size() > listTwo.size() ? listOne.size() : listTwo.size();
// Loop thru the list
for( int i = 0; i <= maxSize; i++){
// We need to check first if index exist then just add it to mergeList
if( i < listOne.size() ) mergedList.add( listOne.get( i ) );
// We need to check first if index exist then just add it to mergeList
if( i < listTwo.size() ) mergedList.add( listTwo.get( i ) );
}
Try this
Iterator iterator1 = arr1.iterator();
Iterator iterator2 = arr2.iterator();
while (iterator1.hasNext() || iterator2.hasNext()) {
if(iterator1.hasNext()){
mergerArr.add(iterator1.next());
}
if(iterator2.hasNext()){
mergerArr.add(iterator2.next());
}
}
Array1= {1,2,3}
Array2= {a,b,c,d,e}
Output= {1, a, 2, b, 3, c, d, e}
public class MergeArray
{
public static void main(String args[])
{
char [] arr1= {'1','2','3'};
char [] arr2= {'a','b','c','d','e'};
int l1= arr1.length;
int l2=arr2.length;
int l3=l1+l2;
char [] arr3=new char[l1+l2];
int i=0;
int j=0;
int k=0;
int m=0;
int r=0;
if(l1<l2)
r=l1;
else
r=l2;
while(m<r)
{
arr3[k++]=arr1[i++];
arr3[k++]=arr2[j++];
m++;
}
while(k<l3)
{
if(l1<l2)
arr3[k++]=arr2[j++];
else
arr3[k++]=arr1[i++];
}
for(int n=0;n<l3;n++)
{
System.out.print(arr3[n]+" ");
}
}
}
I have done this in the following way in php:
<?php
$list1 = array("a","b","c");
$list2 = array(1,2,3);
$list3 = array();
$j=0;
$k=0;
for($i=0;$i<6;$i++)
{
if($i%2==0)
{
$list3[$i]=$list1[$j];
$j++;
}
else
{
$list3[$i]=$list2[$k];
$k++;
}
echo $list3[$i]."<br>";
}
?>
OK, my suggestion is to use ArrayList:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] list1 = in.nextLine().split(",");
String[] list2 = in.nextLine().split(",");
ArrayList<String> merged = new ArrayList<>();
for (int i = 0; i < Math.max(list1.length,list2.length); i++) {
merged.add(list1[i]);
merged.add(list2[i]);
}
System.out.println(String.join(",", merged));
}
You can change the code to Integer if you are sure that the input will be only numbers.

Sort one dimensional array into two dimensional

private double[] myNumbers = {10, 2, 5, 3, 6, 4};
private double[][] result;
private double[][] divideNumbers(double[] derp) {
int j = 0, k = 0;
for (int i=0; i < derp.length; i++) {
if (derp[i] >=4 && derp[i] <=8) {
result[1][j] = derp[i];
j++;
}
else {
result[0][k] = derp[i];
k++;
}
}
//System.out.println(result[0] +" "+ result[1]);
return result;
}
I'm trying to sort the one dimensional array in to a matrix, where numbers between 4 - 8 are in one, and all other numbers are in the other.
1) You're not initializing result[][]. You will get a NullPointerException.
Either loop through myNumbers, count the number of values for each category, and create result[][], or push your values into an ArrayList<Double>[2] and use List.toArray() to convert back to an array.
2) result[][] is declared outside your method. While technically valid, it's generally poor form if there is not a specific reason for doing so. Since you're already returning double[][] you might want to declare a double[][] inside your function to work with and return.
I'm not following exactly what you want, but this should allow your code to work.
class OneDimToTwoDim {
public static void main(String[] args) {
// declare myNumbers one dimensional array
double[] myNumbers = {10, 2, 5, 3, 6, 4};
// display two dimensional array
for (int x = 0; x < myNumbers.length; x++) {
System.out.print("[" + myNumbers[x] + "] "); // Display the string.
}
// pass in myNumbers argument for derp parameter, and return a two dimensional array called resultNumbers
double[][] resultNumbers = OneDimToTwoDim.divideNumbers(myNumbers);
System.out.println(); // Display the string.
System.out.println(); // Display the string.
for (int x = 0; x < resultNumbers.length; x++) {
for (int y = 0; y < resultNumbers[x].length; y++) {
System.out.print("[" + resultNumbers[x][y] + "] "); // Display the string.
}
System.out.println(); // Display the string.
}
}
private static double[][] divideNumbers(double[] derp) {
// declare result to be returned
double[][] result = new double[2][derp.length];
int j = 0, k = 0;
for (int i=0; i < derp.length; i++) {
if (derp[i] >=4 && derp[i] <=8) {
result[1][j] = derp[i];
j++;
}
else {
result[0][k] = derp[i];
k++;
}
}
return result;
}
}
Your result array isn't initialized. Are you getting null pointer exceptions? Is that the problem?
private static double[] myNumbers = {10, 2, 5, 3, 6, 4};
private static double[][] result = new double[2][myNumbers.length];
private static double[][] divideNumbers(double[] derp) {
int j = 0, k = 0;
for (int i=0; i < derp.length; i++) {
if (derp[i] >=4 && derp[i] <=8) {
result[1][j] = derp[i];
j++;
}
else {
result[0][k] = derp[i];
k++;
}
}
result[0] = Arrays.copyOfRange(result[0],0,k);
result[1] = Arrays.copyOfRange(result[1],0,j);
return result;
}

Categories

Resources