我使用以下代码将ArrayList中的元素转换为字符串:
public static List<Location> GPSLocalLocations = new ArrayList<Location>(1);
String mystr = "";
for (int i=0; i<GPSService.GPSLocalLocations.size(); i++)
{
mystr += "\n" + GPSService.GPSLocalLocations.get(i).toString();
}
将我的代码更改为以下内容:
for (int i=0; i<GPSService.GPSLocalLocations.size(); i++)
{
gpslog.append("\n").append(GPSService.GPSLocalLocations.get(i).toString());
}
它导致更少的GC Free消息:)。我的错误,应用程序没有挂起因为...我想因为我多次运行我的ASyncTask ...
但仍然迭代超过1000个元素的ArrayList需要一些时间[大约10秒]。那可以吗?
答案 0 :(得分:0)
我建议使用StringBuilder。
@Override public String [More ...] toString() {
return "Location[mProvider=" + mProvider +
",mTime=" + mTime +
",mLatitude=" + mLatitude +
",mLongitude=" + mLongitude +
",mHasAltitude=" + mHasAltitude +
",mAltitude=" + mAltitude +
",mHasSpeed=" + mHasSpeed +
",mSpeed=" + mSpeed +
",mHasBearing=" + mHasBearing +
",mBearing=" + mBearing +
",mHasAccuracy=" + mHasAccuracy +
",mAccuracy=" + mAccuracy +
",mExtras=" + mExtras + "]";
}
我建议您按照以下方式自行编写:
public static List<Location> GPSLocalLocations = new ArrayList<Location>(1);
StringBuilder gpslog = new StringBuilder();
for (int i=0; i<GPSService.GPSLocalLocations.size(); i++)
{
Location location = GPSService.GPSLocalLocations.get(i);
gpslog.append("\n")
.append("Location[Provider=").append(location.getProvider(())())
.append(",Time=").append(location.getTime())
.append(",Latitude=").append(location.getLatitude())
.append(",Longitude=").append(location.getLongitude())
.append(",HasAltitude=").append(location.getHasAltitude())
.append(",Altitude=").append(location.getAltitude())
.append(",HasSpeed=").append(location.getHasSpeed())
.append(",Speed=").append(location.getSpeed())
.append(",HasBearing=").append(location.getHasBearing())
.append(",Bearing=").append(location.getBearing())
.append(",HasAccuracy=").append(location.getHasAccuracy())
.append(",Accuracy=").append(location.getAccuracy())
.append(",Extras=").append(location.getExtras()).append("]");
}