Java 获取(int,int)参数以接受(int,数组)输入
有没有办法将ArrayList myZips中的ZipCode转换为整数,这样它就可以作为距离方法的参数 ZipCode类:Java 获取(int,int)参数以接受(int,数组)输入,java,Java,有没有办法将ArrayList myZips中的ZipCode转换为整数,这样它就可以作为距离方法的参数 ZipCode类: public class ZipCode { private int zipCode; private String city; private String state; private double longitude; private double latitude; public ZipCode(int pZip)
public class ZipCode
{
private int zipCode;
private String city;
private String state;
private double longitude;
private double latitude;
public ZipCode(int pZip)
{
zipCode = pZip;
city = "UNKOWN";
state = "ST";
latitude = 0.0;
longitude = 0.0;
}
public ZipCode(int pZip, String pCity, String pState, double pLat, double pLon)
{
zipCode = pZip;
city = pCity;
state = pState;
latitude = pLat;
longitude = pLon;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public int getZipCode() {
return zipCode;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public String toString() {
return city + ", " + state + zipCode;
}
}
public class ZipCodeDatabase {
private ArrayList<ZipCode> myZips;
public ZipCodeDatabase()
{
myZips = new ArrayList<ZipCode>();
}
public ZipCode findZip(int zip){
for(ZipCode zipCode : myZips){
if(zipCode.getZipCode() == zip){
return zipCode;
}
}
return null;
}
public double distance(int zip1, int zip2) {
int EARTH_RADIUS = 3959;
if (findZip(zip1) != null && findZip(zip2) != null) {
ZipCode z1 = new ZipCode(zip1);
ZipCode z2 = new ZipCode(zip2);
double lat1 = Math.toRadians(z1.getLatitude());
double long1 = Math.toRadians(z1.getLongitude());
double lat2 = Math.toRadians(z2.getLatitude());
double long2 = Math.toRadians(z2.getLongitude());
double p1 = Math.cos(lat1) * Math.cos(long1) * Math.cos(lat2) * Math.cos(long2);
double p2 = Math.cos(lat1) * Math.sin(long1) * Math.cos(lat2) * Math.sin(long2);
double p3 = Math.sin(lat1) * Math.sin(lat2);
double distance = Math.acos(p1+p2+p3) * EARTH_RADIUS;
int distance1 = (int)Math.round(distance);
return distance1;
}
else {
return -1;
}
}
public ArrayList <ZipCode> withinRadius(int pZip, int pRadius){
ArrayList<ZipCode> zips = new ArrayList<ZipCode> ();
if (findZip(pZip) != null){
for (int i = 0; i < myZips.size(); i++){
if (distance(pZip, myZips.get(i)) <= pRadius) //the trouble is here, how can I convert the myZips.get(I) into the relevant 5 digit integer .
zips.add(pZip);
}
}
return zips;
}
公共类ZipCode
{
私有int-zipCode;
私人城市;
私有字符串状态;
私人双经度;
私人双纬度;
公共ZipCode(int-pZip)
{
zipCode=pZip;
城市=“未知”;
state=“ST”;
纬度=0.0;
经度=0.0;
}
公共ZipCode(int-pZip、String-pCity、String-pState、双平台、双pLon)
{
zipCode=pZip;
城市=城市;
状态=pState;
纬度=平台;
经度=pLon;
}
公共无效设置zipCode(int zipCode){
this.zipCode=zipCode;
}
公共城市(字符串城市){
this.city=城市;
}
公共无效设置状态(字符串状态){
this.state=状态;
}
公共纬度(双纬度){
这个。纬度=纬度;
}
公共经度(双经度){
这个经度=经度;
}
public int getZipCode(){
返回zipCode;
}
公共字符串getCity(){
回归城市;
}
公共字符串getState(){
返回状态;
}
公共双纬度(){
返回纬度;
}
公共双getLongitude(){
返回经度;
}
公共字符串toString(){
返回城市+“,”+州+zipCode;
}
}
公共类ZipCode数据库{
私人ArrayList myZips;
公共ZipCodeDatabase()
{
myZips=newarraylist();
}
公共ZipCode findZip(int-zip){
用于(ZipCode ZipCode:myZips){
if(zipCode.getZipCode()==zip){
返回zipCode;
}
}
返回null;
}
公共双倍距离(整数zip1,整数zip2){
int接地半径=3959;
if(findZip(zip1)!=null和&findZip(zip2)!=null){
ZipCode z1=新ZipCode(zip1);
ZipCode z2=新ZipCode(zip2);
double lat1=数学托拉迪安(z1.getLatitude());
double long1=Math.toRadians(z1.getLongitude());
double lat2=数学托拉迪安(z2.getLatitude());
double long2=Math.toRadians(z2.getLongitude());
双p1=Math.cos(lat1)*Math.cos(long1)*Math.cos(lat2)*Math.cos(long2);
double p2=Math.cos(lat1)*Math.sin(long1)*Math.cos(lat2)*Math.sin(long2);
双p3=数学正弦(lat1)*数学正弦(lat2);
双距离=数学acos(p1+p2+p3)*地球半径;
int距离1=(int)数学圆(距离);
返回距离1;
}
否则{
返回-1;
}
}
公共阵列列表withinRadius(int pZip,int pRadius){
ArrayList zips=新的ArrayList();
如果(findZip(pZip)!=null){
对于(int i=0;i
我猜如果你只需要看两条拉链,你就只需要这个了
ZipCode zip1 = myZips.get(0);
ZipCode zip2 = myZips.get(1);
double d = distance(zip1.getZipCode(), zip2.getZipCode());
如果您想转换整个ZipCode列表,可以这样做
List<Integer> zipCodeInts = new ArrayList<>(myZips.size());
for(ZipCode zipCode : myZips){
zipCodeInts.add(zipCode.getZipCode());
}
// now use zipCodeInts somehow
ZipCode z1 = findZip(zip1);
ZipCode z2 = findZip(zip2);
if(z1 != null && z2 != null) {
// do stuff
}
你可以这样做
List<Integer> zipCodeInts = new ArrayList<>(myZips.size());
for(ZipCode zipCode : myZips){
zipCodeInts.add(zipCode.getZipCode());
}
// now use zipCodeInts somehow
ZipCode z1 = findZip(zip1);
ZipCode z2 = findZip(zip2);
if(z1 != null && z2 != null) {
// do stuff
}
我写的!:)
我猜如果你只需要看两条拉链,你就只需要这个了
ZipCode zip1 = myZips.get(0);
ZipCode zip2 = myZips.get(1);
double d = distance(zip1.getZipCode(), zip2.getZipCode());
如果您想转换整个ZipCode列表,可以这样做
List<Integer> zipCodeInts = new ArrayList<>(myZips.size());
for(ZipCode zipCode : myZips){
zipCodeInts.add(zipCode.getZipCode());
}
// now use zipCodeInts somehow
ZipCode z1 = findZip(zip1);
ZipCode z2 = findZip(zip2);
if(z1 != null && z2 != null) {
// do stuff
}
你可以这样做
List<Integer> zipCodeInts = new ArrayList<>(myZips.size());
for(ZipCode zipCode : myZips){
zipCodeInts.add(zipCode.getZipCode());
}
// now use zipCodeInts somehow
ZipCode z1 = findZip(zip1);
ZipCode z2 = findZip(zip2);
if(z1 != null && z2 != null) {
// do stuff
}
只需从类中获取包含邮政编码的int
:
ZipCodeDatabase.distance(myZipCode.getZipCode(), yourZipCode.getZipCode());
只需从类中获取包含邮政编码的int
:
ZipCodeDatabase.distance(myZipCode.getZipCode(), yourZipCode.getZipCode());
我不理解拥有findZip
的背后的原因,因为我认为你做的查找比你需要的要多,但无论如何,你很接近,只需要保存一个对ZipCode
的引用,你从findZip
找到它,然后在distance
方法中调用getZipCode()
public static final int EARTH_RADIUS = 3959; // Move constants out of methods
public ZipCode findZip(int zip){
for(ZipCode zipCode : myZips){
if(zipCode.getZipCode() == zip){
return zipCode;
}
}
return null;
}
public int distance(int zip1, int zip2){
// save these references so you can use them later
ZipCode z1 = findZip(zip1);
ZipCode z2 = findZip(zip2);
if(z1 != null && z2 != null) {
double lat1 = Math.toRadians(z1.getLatitude());
double long1 = Math.toRadians(z1.getLongitude());
double lat2 = Math.toRadians(z2.getLatitude());
double long2 = Math.toRadians(z2.getLongitude());
double p1 = Math.cos(lat1) * Math.cos(long1) * Math.cos(lat2) * Math.cos(long2);
double p2 = Math.cos(lat1) * Math.sin(long1) * Math.cos(lat2) * Math.sin(long2);
double p3 = Math.sin(lat1) * Math.sin(lat2);
double distance = Math.acos(p1+p2+p3) * EARTH_RADIUS;
return (int)Math.round(distance);
}
else return -1;
}
public ArrayList <ZipCode> withinRadius(int pZip, int pRadius){
ZipCode zip = findZip(pZip); // save this reference to calculate the distances
ArrayList<ZipCode> zips = new ArrayList<ZipCode> ();
if (zip != null) {
for (int i = 0; i < myZips.size(); i++) {
ZipCode z = myZips.get(i); // or use a for-each loop like in findZip
if (distance(zip.getZipCode(), z.getZipCode()) <= pRadius) {
zips.add(z);
}
}
}
return zips;
}
public static final int EARTH_RADIUS=3959;//将常量移出方法
公共ZipCode findZip(int-zip){
用于(ZipCode ZipCode:myZips){
if(zipCode.getZipCode()==zip){
返回zipCode;
}
}
返回null;
}
公共整数距离(整数zip1,整数zip2){
//保存这些引用,以便以后使用
ZipCode z1=findZip(zip1);
ZipCode z2=findZip(zip2);
如果(z1!=null&&z2!=null){
double lat1=数学托拉迪安(z1.getLatitude());
double long1=Math.toRadians(z1.getLongitude());
double lat2=数学托拉迪安(z2.getLatitude());
double long2=Math.toRadians(z2.getLongitude());
双p1=Math.cos(lat1)*Math.cos(long1)*Math.cos(lat2)*Math.cos(long2);
double p2=Math.cos(lat1)*Math.sin(long1)*Math.cos(lat2)*Math.sin(long2);
双p3=数学正弦(lat1)*数学正弦(lat2);
双距离=数学acos(p1+p2+p3)*地球半径;
返回(整数)数学圆(距离);
}
否则返回-1;
}
公共阵列列表withinRadius(int pZip,int pRadius){
zipcodezip=findZip(pZip);//保存此引用以计算距离
ArrayList zips=新的ArrayList();
if(zip!=null){
对于(int i=0;i if(distance(zip.getZipCode(),z.getZipCode())我不理解拥有findZip
背后的原因,因为我认为你做的查找比你需要的要多,但无论如何,你很接近,只需要保存一个对findZip
中找到的ZipCode
的引用,然后调用getZipCode()
中的距离方法
public static final int EARTH_RADIUS = 3959; // Move constants out of methods
public ZipCode findZip(int zip){
for(ZipCode zipCode : myZips){
if(zipCode.getZipCode() == zip){
return zipCode;
}
}
return null;
}
public int distance(int zip1, int zip2){
// save these references so you can use them later
ZipCode z1 = findZip(zip1);
ZipCode z2 = findZip(zip2);
if(z1 != null && z2 != null) {
double lat1 = Math.toRadians(z1.getLatitude());
double long1 = Math.toRadians(z1.getLongitude());
double lat2 = Math.toRadians(z2.getLatitude());
double long2 = Math.toRadians(z2.getLongitude());
double p1 = Math.cos(lat1) * Math.cos(long1) * Math.cos(lat2) * Math.cos(long2);
double p2 = Math.cos(lat1) * Math.sin(long1) * Math.cos(lat2) * Math.sin(long2);
double p3 = Math.sin(lat1) * Math.sin(lat2);
double distance = Math.acos(p1+p2+p3) * EARTH_RADIUS;
return (int)Math.round(distance);
}
else return -1;
}
public ArrayList <ZipCode> withinRadius(int pZip, int pRadius){
ZipCode zip = findZip(pZip); // save this reference to calculate the distances
ArrayList<ZipCode> zips = new ArrayList<ZipCode> ();
if (zip != null) {
for (int i = 0; i < myZips.size(); i++) {
ZipCode z = myZips.get(i); // or use a for-each loop like in findZip
if (distance(zip.getZipCode(), z.getZipCode()) <= pRadius) {
zips.add(z);
}
}
}
return zips;
}
public static final int EARTH_RADIUS=3959;//将常量移出方法
公共ZipCode findZip(int-zip){
用于(ZipCode ZipCode:myZips){
if(zipCode.getZipCode()==zip){
返回zipCode;
}
}
返回null;
}
公共整数距离(整数zip1,整数zip2){
//保存这些引用,以便您可以使用它们