Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Trying improve this algorithm(Java)

So I have two 2 dimensional arrays that need to be populated from the same initial string array.

 

Given:

String[] input_array//size 54

double[3][9] array1;

double int[3][9] array2;

 

the pattern of input array basically is the first 9 elements map to 1st array in array1, next map to 1st array in array2 and then the pattern repeats.

 

The best I can come up with is:

 

LinkedList<String> inputList = new LinkedList<>(Arrays.asList(input_array);

for(int i = 0; i < array1.length; i++)
{
	for(int j = 0; j < 9; j++)
    {
    	array1[i][j] = Double.parseDouble(inputList.pop());
    }
    for(int j = 0; j < 9; j++)
    {
    	array1[i][j] = Integer.parseInt(inputList.pop());
    }
}

This code works how I want it to, however I don't like that I'm nest two for loops into another for loop.

There has to be a better and cleaner way to do this.

 

Full disclosure this is for work, just trying to improve my algorithm

Link to post
Share on other sites

as far as i'm aware, a loop in a loop is the way to populate a two-dimensional array.

 

also, you're populating two two-dimensional arrays in 6 lines of code, that's pretty darn clean...

Link to post
Share on other sites

Using LinkedList pop this is the most efficient method to iterate over a 2D array, that's just what the structure of a 2D array requires. Don't optimize before you know you need to.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 20 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
11 minutes ago, manikyath said:

as far as i'm aware, a loop in a loop is the way to populate a two-dimensional array.

 

also, you're populating two two-dimensional arrays in 6 lines of code, that's pretty darn clean...

 

10 minutes ago, BobVonBob said:

Using LinkedList pop this is the most efficient method to iterate over a 2D array, that's just what the structure of a 2D array requires. Don't optimize before you know you need to.

Getting things right on the first try isn't something I'm used to doing haha. Thank you for the inputs

Link to post
Share on other sites
5 hours ago, BrownZeus said:

There has to be a better and cleaner way to do this

You have a bug in your original code: you assigned to array1 in both loops and never used array2 at all.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

No idea if this is actually any faster than the original code -- probably not -- but it squeezes the code in slightly smaller space:

LinkedList<String> inputList = new LinkedList<>(Arrays.asList(input_array);

for(int i = 0; i < array1.length; i++)
{
	for(int j = 0; j < 9; j++)
    {
    	array1[i][j] = Double.parseDouble(inputList[i * 9 * 2]);
    	array2[i][j] = Integer.parseInt(inputList[i * 9 * 2 + 9]);
    }
}

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Maybe if you could explain why exactly it is that you're doing what you're doing we can jump in with better approaches. Also give Streams a try.

Link to post
Share on other sites
1 hour ago, doertedev said:

Maybe if you could explain why exactly it is that you're doing what you're doing we can jump in with better approaches. Also give Streams a try.

Obviously it's his homework.

Link to post
Share on other sites
17 hours ago, WereCatf said:

You have a bug in your original code: you assigned to array1 in both loops and never used array2 at all.

You're right, its a typo!

 

15 hours ago, doertedev said:

Maybe if you could explain why exactly it is that you're doing what you're doing we can jump in with better approaches. Also give Streams a try.

Basically I have to take an input of String[], and then the pattern basically is first 9 populate a double array, and then the next 9 populate an int array, and that cycle repeats for a total of 6 arrays total. I did give streams a try, and I have an update! See below!

 

 

14 hours ago, Lordess said:

Obviously it's his homework.

If you look at my OP, i disclaimed right there this is for work, as in my job. If it was homework I wouldn't give rat's behind about algorithm efficiency, or quality. Like most CS majors in the US these days, I got my degree on trash code and caffeine.

 

 

That being said the reason I was doing 2 dimensional arrays was due to a data structure I already had to work with (custom pojos and all), so my solution to get out of using 3 loops was streams!
 

Turned the String[] into a LinkedList and then did this:

 

LinkedList<String> inputList = new LinkedList<>(Arrays.asList(input_array);

for(int i = 0; i < array1.length; i++)
{
	
    	array1[i] = IntStream.range(0, 9).mapToDouble(i -> Double.parseDouble(inputList.pop())).toArray();
    	array2[i] = IntStream.range(0, 9).map(i -> Integer.parseInt(inputList.pop())).toArray();
    
}

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×