Java 如何使用ArrayList和For循环,然后将循环结果放入数组中?
我有海岸线坐标(超过一百个坐标),我想把坐标放到数组中。 然后计算当前位置到所有坐标之间的距离(使用for循环),并将结果放入数组中。 所以我有两个数组Java 如何使用ArrayList和For循环,然后将循环结果放入数组中?,java,android,arrays,Java,Android,Arrays,我有海岸线坐标(超过一百个坐标),我想把坐标放到数组中。 然后计算当前位置到所有坐标之间的距离(使用for循环),并将结果放入数组中。 所以我有两个数组 地理点阵列 距离阵列 我想找到距离数组的最小值,但程序总是强制关闭。这是我的代码,只显示十个坐标: public class Nav extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { su
public class Nav extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
List<GeoPoint> points = new ArrayList<GeoPoint>();
Double latitude1 = Double.parseDouble("-0.696928");
Double longitude1 = Double.parseDouble("119.843473");
GeoPoint latLong1 = new GeoPoint((int)(latitude1*1e6),(int)(longitude1*1e6));
Double latitude2 = Double.parseDouble("-0.699331");
Double longitude2 = Double.parseDouble("119.84416");
GeoPoint latLong2 = new GeoPoint((int)(latitude2*1e6),(int)(longitude2*1e6));
Double latitude3 = Double.parseDouble("-0.701048");
Double longitude3 = Double.parseDouble("119.843817");
GeoPoint latLong3 = new GeoPoint((int)(latitude3*1e6),(int)(longitude3*1e6));
Double latitude4 = Double.parseDouble("-0.702592");
Double longitude4 = Double.parseDouble("119.843817");
GeoPoint latLong4 = new GeoPoint((int)(latitude4*1e6),(int)(longitude4*1e6));
Double latitude5 = Double.parseDouble("-0.701563");
Double longitude5 = Double.parseDouble("119.84622");
GeoPoint latLong5 = new GeoPoint((int)(latitude5*1e6),(int)(longitude5*1e6));
Double latitude6 = Double.parseDouble("-0.702249");
Double longitude6 = Double.parseDouble("119.84725");
GeoPoint latLong6 = new GeoPoint((int)(latitude6*1e6),(int)(longitude6*1e6));
Double latitude7 = Double.parseDouble("-0.703451");
Double longitude7= Double.parseDouble("119.846907");
GeoPoint latLong7 = new GeoPoint((int)(latitude7*1e6),(int)(longitude7*1e6));
Double latitude8 = Double.parseDouble("-0.705167");
Double longitude8 = Double.parseDouble("119.847937");
GeoPoint latLong8 = new GeoPoint((int)(latitude8*1e6),(int)(longitude8*1e6));
Double latitude9 = Double.parseDouble("-0.70654");
Double longitude9 = Double.parseDouble("119.849653");
GeoPoint latLong9 = new GeoPoint((int)(latitude9*1e6),(int)(longitude9*1e6));
Double latitude10 = Double.parseDouble("10. -0.707914");
Double longitude10 = Double.parseDouble("119.85137");
GeoPoint latLong10 = new GeoPoint((int)(latitude10*1e6),(int)(longitude10*1e6));
points.add(latLong1);
points.add(latLong2);
points.add(latLong3);
points.add(latLong4);
points.add(latLong5);
points.add(latLong6);
points.add(latLong7);
points.add(latLong8);
points.add(latLong9);
points.add(latLong10);
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
Double lat,lon;
lat = location.getLatitude ();
lon = location.getLongitude ();
Location locationA = new Location("USER");
locationA.setLatitude(lat);
locationA.setLongitude(lon);
ArrayList<Double> dist = new ArrayList<Double>();
for(int i=0; i<=points.size(); i++){
double distances = locationA.distanceTo(points(i));
dist.add(distances);
double minDist = Collections.min(dist);
Toast.makeText( getApplicationContext(),
"Your Distance to the beach is " + minDist,
Toast.LENGTH_LONG).show();
}
}
private Location points(int i) {
// TODO Auto-generated method stub
return null;
}
}
公共类导航扩展了碎片活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
列表点=新的ArrayList();
Double-latitude1=Double.parseDouble(“-0.696928”);
Double longitude1=Double.parseDouble(“119.843473”);
地质点板条1=新的地质点((int)(纬度1*1e6),(int)(纵向1*1e6));
Double-latitude2=Double.parseDouble(“-0.699331”);
Double longitude2=Double.parseDouble(“119.84416”);
地质点latLong2=新的地质点((int)(纬度2*1e6),(int)(纵向2*1e6));
Double-latitude3=Double.parseDouble(“-0.701048”);
Double longitude3=Double.parseDouble(“119.843817”);
地质点latLong3=新的地质点((int)(纬度3*1e6),(int)(纵向3*1e6));
Double-latitude4=Double.parseDouble(“-0.702592”);
Double longitude4=Double.parseDouble(“119.843817”);
地质点latLong4=新的地质点((int)(纬度4*1e6),(int)(纵向4*1e6));
Double-latitude5=Double.parseDouble(“-0.701563”);
Double longitude5=Double.parseDouble(“119.84622”);
地质点latLong5=新的地质点((int)(纬度5*1e6),(int)(纵向5*1e6));
Double-latitude6=Double.parseDouble(“-0.702249”);
Double longitude6=Double.parseDouble(“119.84725”);
地质点latLong6=新的地质点((int)(纬度6*1e6),(int)(纵向6*1e6));
Double-latitude7=Double.parseDouble(“-0.703451”);
Double longitude7=Double.parseDouble(“119.846907”);
地质点latLong7=新的地质点((int)(纬度7*1e6),(int)(纵向7*1e6));
Double-latitude8=Double.parseDouble(“-0.705167”);
Double longitude8=Double.parseDouble(“119.847937”);
地质点latLong8=新的地质点((int)(纬度8*1e6),(int)(纵向8*1e6));
Double-latitude9=Double.parseDouble(“-0.70654”);
Double longitude9=Double.parseDouble(“119.849653”);
地质点latLong9=新的地质点((int)(纬度9*1e6),(int)(纵向9*1e6));
双纬度10=Double.parseDouble(“10.-0.707914”);
Double longitude10=Double.parseDouble(“119.85137”);
地质点板条10=新的地质点((int)(纬度10*1e6),(int)(纵向10*1e6));
加分(latLong1);
加分(latLong2);
加分(latLong3);
加分(latLong4);
加分(latLong5);
加分(latLong6);
加分(latLong7);
加分(latLong8);
加分(latLong9);
加分(latLong10);
//获取位置管理器
LocationManager LocationManager=(LocationManager)getSystemService(LOCATION\u服务);
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
标准。setAltitudeRequired(false);
Location Location=locationManager.getLastKnownLocation(locationManager.getBestProvider(标准,true));
双lat,lon;
lat=location.getLatitude();
lon=location.getLongitude();
位置A=新位置(“用户”);
位置a.设置纬度(纬度);
位置a.经度(lon);
ArrayList dist=新的ArrayList();
对于(int i=0;i来说,听起来您正在获得ArrayIndexOutOfBoundsException
for(int i=0;i听起来你好像得到了ArrayIndexOutOfBoundsException
for(int i=0;i如果看不到错误消息,我肯定能发现问题。您正在遍历列表的末尾。使用如果看不到错误消息,我肯定能发现问题。您正在遍历列表的末尾。使用LocationManager LocationManager=(LocationManager)getSystemService(地点和服务)
使用getApplicationContext().getSystemService(位置服务)
或Context.getSystemService(位置服务);位置管理器位置管理器=(位置管理器)getSystemService(位置服务)
使用getApplicationContext().getSystemService(位置服务)
或Context.getSystemService(位置\服务)它在哪一行崩溃?强制关闭很可能意味着异常。所以你也可以在这里发布你的异常。你可以从LogCat获得它。我已经显示了我的LogCat@husyn抱歉,我不知道哪个是崩溃。但是你可以看到我的LogCat@kcGibson在Nav.jar的第32行是什么?我对LogCat所指的与以下相关的内容感兴趣:“在com.marker.Nav.onCreate(Nav.java:32)“它在哪一行崩溃?强制关闭最有可能意味着异常。所以你也可以在这里发布你的异常。你可以从LogCat获得它。我已经显示了我的LogCat@husyn抱歉,我不知道哪一行是崩溃。但是你可以看到我的LogCat@kCgibsonNav.jar的第32行是什么?我对LogCat所指的与以下相关的内容感兴趣:“在com.marker.Nav.onCreate(Nav.java:32)”实际上我不知道我必须把Collections.min()放在哪里。我想做的是,如果所有的距离都已经在distance数组中,我想找到Collections.min()距离数组的。我已经显示了我的日志。你能告诉我哪一个是错的吗?@vidya实际上我不知道我必须把集合放在哪里。min()。我想做的是,如果所有的距离都已经在距离数组中,我想
10-08 13:20:18.784: I/Process(632): Sending signal. PID: 632 SIG: 9
10-08 13:20:31.743: D/dalvikvm(675): GC_FOR_ALLOC freed 254K, 10% free 5364K/5959K, paused 95ms, total 99ms
10-08 13:20:31.993: I/Choreographer(675): Skipped 59 frames! The application may be doing too much work on its main thread.
10-08 13:20:32.024: D/gralloc_goldfish(675): Emulator without GPU emulation detected.
10-08 13:20:33.944: I/Choreographer(675): Skipped 159 frames! The application may be doing too much work on its main thread.
10-08 13:20:34.084: D/AndroidRuntime(675): Shutting down VM
10-08 13:20:34.084: W/dalvikvm(675): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-08 13:20:34.174: E/AndroidRuntime(675): FATAL EXCEPTION: main
10-08 13:20:34.174: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marker/com.marker.Nav}: java.lang.RuntimeException: stub
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 13:20:34.174: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 13:20:34.174: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 13:20:34.174: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 13:20:34.174: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 13:20:34.174: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method)
10-08 13:20:34.174: E/AndroidRuntime(675): Caused by: java.lang.RuntimeException: stub
10-08 13:20:34.174: E/AndroidRuntime(675): at com.google.android.maps.GeoPoint.(Unknown Source)
10-08 13:20:34.174: E/AndroidRuntime(675): at com.marker.Nav.onCreate(Nav.java:32)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.Activity.performCreate(Activity.java:5008)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-08 13:20:34.174: E/AndroidRuntime(675): ... 11 more
for(int i=0; i<=points.size(); i++){ // Check i<= here it should be < only
for(int i=0; i<points.size(); i++){