• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to loop through an ArrayList with 100 doubles and select the smallest ones from each tenth?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

1. I got an ArrayList containing 100 distances as doubles.
2. I need a method that returns one value at a time.
3. the value has to be the smallest one from each tenth.
4. What to do if the are duplicates?

For example:

    ArrayList
     {10,21,12,23,34,5,61,72,18,9}   //min value = 5
     {21,36,12,45,85,45,6,89,53,11}  //min value = 6
     {1,23,22,11,3,44,54,44,1,2,33}  //min value = 1

...till the end os the list

Here is the code:

 
Marshal
Posts: 25195
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marin Capranov wrote:2. I need a method that returns one value at a time.



A method returns a value. That's all it can do. "One value at a time" doesn't make sense in Java. So, could you post a copy of your actual requirements for the method?
 
Saloon Keeper
Posts: 6923
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see where you are dealing with  1/10th of the list each time it is called. You'd have to keep track of which 1/10th you processed last time.
 
Marin Capranov
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not dealt with the list in a way that it takes every tenth from the ArrayList[100]. Basically, sounds like a list of sublists, and each sublist should return the smallest value. But the return of the method should return the smallest value of the sublist[0], then sublist[1] and so on until sublist[9]. Like a loop iteration and per each iteration, which is 10 in total.
 
Paul Clapham
Marshal
Posts: 25195
64
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a method which scans a list and looks at a sublist to return the smallest value in that sublist, then you're going to need a method to which you pass the list and information about how to find the sublist.

Or alternatively you could use the sublist(fromIndex, toIndex) method from the standard Java API.
 
Marin Capranov
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for editing. Did not think about the consequences.

Basically, I have tried to implement the suggested subLists with a minimum value finder method. Something went wrong because it returns only 0's.

 
Carey Brown
Saloon Keeper
Posts: 6923
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this.xAndY.size() shouldn't be changing so using this in your if()'s is the wrong thing to do.

Could you post the complete class so we have some context.
 
Marin Capranov
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding XandY is just for LatLng, my bad.

Also, this is the entire class, however, don't know if will say much:



That is the loop from another that actually passes those distances through:



Those distances are parsed from a distance matrix api provided by Google.
I am using those distances for my GA to find the shortest path and solve the TSP.

 
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: I started writing this post two hours ago. I was responding to the following post:

Marin Capranov wrote:I have not dealt with the list in a way that it takes every tenth from the ArrayList[100]. Basically, sounds like a list of sublists, and each sublist should return the smallest value. But the return of the method should return the smallest value of the sublist[0], then sublist[1] and so on until sublist[9]. Like a loop iteration and per each iteration, which is 10 in total.



So really, you want the method to return either an Iterable or a Stream.

Sorry to be blunt, but your code is a mess. What does Distance represent? What does xAndY represent, and why does it contain more than 2 elements, or if each individual element is supposed to represent an x and a y, then why is the element type double? If the method distances() is supposed to return the smallest of every ten elements of two lists, then why is it named so poorly?

It's not clear to me what you want exactly, I think it's something like this?

Except both lists contain 100 elements, and you want to get the least of each 10. Here are the methods I wrote to implement this:
 
Bartender
Posts: 3769
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave it a try too, although the comparing of two lists is not yet implemented. And I presumed that T implements Comparable<T>, for brevity, with the min as fixed operator.
 
Carey Brown
Saloon Keeper
Posts: 6923
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

distance matrix api provided by Google.


Where is the API documentation for this? Do you have a URL?
 
Carey Brown
Saloon Keeper
Posts: 6923
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

GA to find the shortest path and solve the TSP.


I assume "GA" is genetic algorithm and "TSP" is traveling salesman problem. Your prior post involved finding the shortest path between two points. This is different than the TSP where you need the shortest distance to visit ALL points.
 
Marin Capranov
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got the GA implemented already.

All I have got a matrix of distances so I just simply want to find the shortest path visiting all places.

I am nearly there just stuck on the part I have created the current topic on.

Regarding the API documentation is the google Distance Matrix API. I am not sure if I understood your question on that=)
 
Marin Capranov
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I develop in android 6.0.1 and it does not support streams, hence Java 8.

Would be good to substitute it with java 7 at max.

Also, can it be less verbose the code? Would be good if it fits inside a method only, Stephen=)
 
Carey Brown
Saloon Keeper
Posts: 6923
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marin Capranov wrote:Regarding the API documentation is the google Distance Matrix API. I am not sure if I understood your question on that=)


Google needs a billing account before they'll even show you what the API is, so this is a dead end unless you can cut and paste the particular ones you are using.

I find it hard to believe that an API would force you to take one long string of distances and turn it into a matrix yourself.
 
Stephan van Hulst
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marin Capranov wrote:By the way, I develop in android 6.0.1 and it does not support streams, hence Java 8.


Well, you really should not be doing all this in Android anyway. As the Maps API documentation itself has already pointed out, you should access the API through a proxy service where you do all the processing, and then your Android client just delegates all operations to your proxy service.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!