Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I have a Unit class which has lots of fields in it as shown below:
Unit
public class Unit { private final int id; private final int beds; private final String city; private final double lat; private final double lon; // constructors and getters here // toString method }
I now have a List of Unit which is a List object which contains lots of Units. Now I need to find the nearest Units from List object to Unit x. Cap the results by limit.
List
Unit x
private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) { List<Unit> output = new ArrayList<>(); // how do I sort lists object in such a way so that I can get nearest units here to "x"? return output; }
We have lat/long present in the Unit class so we can use that to calculate euclidean distance and do the comparison. I am confused on how to sort the list of units by shortest distance and get the nearest units. I am working with Java 7 as of now so I can't use Java 8.
// this distance method reference from https://stackoverflow.com/a/16794680/6138660
public static double distance(double lat1, double lat2, double lon1, double lon2) { final int R = 6371; // Radius of the earth double latDistance = Math.toRadians(lat2 - lat1); double lonDistance = Math.toRadians(lon2 - lon1); double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double distance = R * c * 1000; // convert to meters distance = Math.pow(distance, 2); return Math.sqrt(distance); } private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) { lists.sort(new Comparator<Unit>() { @Override public int compare(Unit o1, Unit o2) { double flagLat = x.getLat(); double flagLon = x.getLon(); double o1DistanceFromFlag = distance(flagLat, o1.getLat(), flagLon, o1.getLon()); double o2DistanceFromFlag REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.