Jump to content

Trying improve this algorithm(Java)

BrownZeus

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 comment
Share on other sites

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 comment
Share on other sites

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-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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

×