4

I want to create a multidimensional array where each of the nodes will have the following details:

  1. Place Name ex: "Mysore" , "Bangalore"
  2. Icon name ex: "waterfall", "wildlife"
  3. Place Distance ex: "200", "123"

Which is the best way to do this when I have over 30 values ?

Example:

"Bangalore", "200", "city"

"Mysore", "100", "historic"

I am trying to populate a list array in Android where each row has three details - name, icon, distance so I want to temp populate that data in my java class.

9
  • 1
    mean one item having multiple place name? For example: Harsha is having 2 places. Is it the case? Dec 26, 2012 at 10:05
  • what operation you going to perform on your data ? Also pls answer @PareshMayani question.
    – rai.skumar
    Dec 26, 2012 at 10:08
  • 1
    Can't you define a class and use an arraylist of that?
    – Swapnil
    Dec 26, 2012 at 10:08
  • That depends on what you feel is the best, you are looking for performance or clean src code or anything else?
    – Jayamohan
    Dec 26, 2012 at 10:08
  • it's not clear what do you want specifically. Best in what way? why multi-dimension array
    – vishal_aim
    Dec 26, 2012 at 10:12

4 Answers 4

22

Don't use a multidimensional array. Use a simple array (or List) of objects:

public class Place {
    private String name;
    private String icon;
    private int distance;
    // constructor, methods skipped for brevity
}

...

private Place[] places = new Place[10];
// or
private List<Place> places = new ArrayList<Place>();

Java is an OO language. Learn to define and use objects.

3
  • JBNizet now how do i add the data into places ?
    – Harsha M V
    Dec 26, 2012 at 10:20
  • Using a constructor (that I didn't show for brevity). If you don't know what a constructor is, then step back, find a good introductory book or tutorial about Java, and learn the basics before starting any work on Android, because that's extremely basic stuff, and you'll hit walls every time if you don't know them. You could start with the Java tutorial
    – JB Nizet
    Dec 26, 2012 at 10:24
  • 1
    I'd suggest to change array to ArrayList. Dec 26, 2012 at 10:58
3

I think best option is create custom defined object.

Class TestObject
{
    String Place,Icon,Distance;
    // Setter and Getter method
}

// Create object of class. Store your value using setter and getter method 
   and save object into list

List<TestObject> test = new ArrayList<TestObject>();
test.add(testObject); //
2

Create a class with the attributes that you want in an element.

now you can create an array of objects of this class.

class Place {
private String name;
private String icon;
private int distance;

public Place(String name,String icon,int distance){
  this.name=name;
  this.icon=icon;
  this.distance=distance;

} 

}

Place places[]=new Place[10];
places[0]=new Place("Mysore","wildlife",123); 
and so on

beware of instantiating the objects else you will endup getting NullPointerException

1

If you don't want to create a separate class . You can also use JSON for your purpose.
JSON object is light weight and can manage aaray data very easily.

Like :

    JSONObject jo = new JSONObject();
    JSONArray ja = new JSONArray();
    ja.put("Mysore");
    ja.put("wildlife");
    ja.put(123);
    jo.add(KEY, ja); // Adding array to JSON Object with key [KEY can be any unique value]

JSON are easy to read and manageable.

It provides lots of functionality rather than array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.