How to use vectors / arrays in Java?

3

Hello, I am very lay in Java and I need to use one variable storing several others. How can I do this? I have the following:

LatLng ponto1 = new LatLng(-19.924312,-43.931762);
LatLng ponto2 = new LatLng(-18.851388,-41.946910);

I'm used to C, where I could do something like:

LatLng pontos[2];
LatLng pontos[1] = new LatLng(-19.924312,-43.931762);
LatLng pontos[2] = new LatLng(-18.851388,-41.946910);

But when I try this in Android Studio, it returns me syntax errors ... How would the correct way to do it?

Note: I need to do this to compare several "points" inside a "for", I think this is the best way ...

--- EDIT ---

It returns me errors of the type:

  • Unkown class: points
  • Missing method body, or declare abstract

- EDIT2-- (code, as bigown requested)

public class Mapa extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mapa);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

LatLng ponto1 = new LatLng(-19.924312,-43.931762);
LatLng ponto2 = new LatLng(-18.851388,-41.946910);

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

if (ActivityCompat.checkSelfPermission(Mapa.this,     android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Mapa.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(Mapa.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }else{
        if(!mMap.isMyLocationEnabled())
            mMap.setMyLocationEnabled(true);

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (myLocation == null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            myLocation = lm.getLastKnownLocation(provider);
        }

        if(myLocation!=null){
            LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
            double distancia = SphericalUtil.computeDistanceBetween(ponto1, userLocation);
            double valorLatitude = myLocation.getLatitude();
            double valorLongitude = myLocation.getLongitude();

            /*String stringLongitude = Double.toString(valorLongitude);
            String stringLatitude = Double.toString(valorLatitude);*/
            String stringDistancia = Double.toString(distancia);

            TextView textLatitude = (TextView) findViewById(R.id.textLatitude);
            textLatitude.setText(stringDistancia);
            /*TextView textLongitude = (TextView) findViewById(R.id.textLongitude);
            textLongitude.setText(stringLongitude);*/
        }
    }
}

I'm using the 'SphericalUtil.computeDistanceBetween' function to calculate the distance from point1 to the user's position. This works normally ... Now I needed a 'for' to calculate the distance from each point I listed above in relation to the user's position and then return me the shortest distance ... For this, I think I would need to put all the points in a single array and test element by element of the array, but I do not know how to declare the array up there ...

    
asked by anonymous 08.10.2016 / 04:24

2 answers

4

In Java, learn that the base language is this, just the base, one of the great strengths of Java is its built-in libraries that do 1001 things, and all come pre-made in JDK.

An Array, the way you're used to in C would be:

Object[] array = new Object[10];

This creates an empty array of type Object of 10 indices, empty. But in newer versions of java we have the 'shortcut':

String[] array = {"um elemento", "outro elemento"}

Initializing an array with size 2 and already with two elements inside, the pattern can apply to any other type of variable. Numbers, Objects:

{new LatLang(10.22, -2.44),new LatLang(1.22, -4.66)}  

etc ... But in Java 8 is much more common the more "modern" version of java sets Lists, Maps, Sets, ETC

The equivalent of arrays in our set library would be ArrayList , the difference is that sets are objects, have dynamic sizes and support various built-in operations: sorting, sorting, searching, etc. They are part of JDK and you can use:

ArrayList<Object> lista = new ArrayList(); 
// E para adicionar items:
lista.add(new Object());
// Voce pode ate especificar o indice
lista.add(3, new Object());
// Mas cuidado, as listas se por mais que listas se expandam sozinhas para 
// acomodar novos indices elas tem índice. o padrão inicial é 10
// voce nao pode acabar de criar uma lista e:
lista.add(10000, new Object());
    
08.10.2016 / 05:02
3

Create a list:

List<LatLng> pontos = new ArrayList<LatLng>();
pontos.add(new LatLng(-19.924312,-43.931762));
pontos.add(new LatLng(-18.851388,-41.946910));

Then recover by pontos.get(0) , for example.

    
08.10.2016 / 04:41