Reputation: 62494
In my function, in order to accept an array of values I have to define a data type...
myFunc( String[] myVar ) {}
What if I wanted an array like the below array that mixes objects and strings and nested arrays/ (Note: the below array is in PHP)
myFunc(array(
array(
'name' => 'Category1',
'products' => array(
array(
productObject,
productObject,
...
)
)
)
));
Is this possible in Java or is this a completely wrong technique?
Upvotes: 2
Views: 3832
Reputation: 24177
Most PHP code is not as strongly typed as code in a more "traditional" OO language like C++, C#, Java, etc. So an approach of porting PHP directly to Java is probably off on the wrong track.
Instead, try building a class to represent your data type. Start from the innermost type, which in your case seems to be a "Product". Example:
public class Product {
private int id;
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
}
Now you need to represent what appears to be a category of products, so you can create another class to hold those. Example:
public class ProductCategory {
private String name;
private List<Product> products;
public ProductCategory(String name) {
this.name = name;
this.products = new ArrayList<Product>();
}
public String getName() {
return this.name;
}
public List<Product> getProducts() {
return this.products;
}
}
To use these classes, you might write code like this:
Product p1 = new Product(1, "P1");
Product p2 = new Product(2, "P2");
Product p3 = new Product(3, "P3");
ProductCategory c1 = new ProductCategory("C1");
// Add product to category 1
c1.getProducts().add(p1);
ProductCategory c2 = new ProductCategory("C2");
// Add products to category 2
c2.getProducts().add(p2);
c2.getProducts().add(p3);
Upvotes: 1
Reputation: 1487
It's possible with an array of Object
, which is the base type in Java, but for your use case a custom class is more javaish.
class Category {
String name;
List<Product> products;
}
With that approach you have a type save way to define your data.
Upvotes: 0
Reputation: 26530
You would need to use an object-oriented approach in Java (which will also work in PHP). The class structure for the object you've listed above would be as follows:
public class MyObject {
String name;
Product[] products;
public MyObject(String name, Product[] products) {
this.name = name;
this.products = products;
}
}
Then you can get an instance of that class by doing the following:
new MyObject("Category1", new Product[] { productObject1, productObject2 });
You can also have an array of this object type:
MyObject[] myObjs = {
new MyObject("Category1", new Product[] { productObject1, productObject2 });
new MyObject("Category2", new Product[] { productObject3, productObject4 });
new MyObject("Category3", new Product[] { productObject5, productObject6 });
}
Upvotes: 1
Reputation: 7435
You can create an Object
array, which would happily take multiple data types. You'll have to be careful with how you cast them to use them though - using instanceof
is handy, or if you know exactly what data type to expect you can cast without the need to check the data type first. It's considered bad practice to use multiple data type arrays, although there's nothing stopping you from doing it if your implementation requires it.
Upvotes: 0
Reputation: 406125
It's possible (an array of Object
), but it's still not a good solution.
It sounds like instead of an array that holds different types, you really want to create a new class. You have names defined for the array elements in your example, so that indicates that you know what types they should be. My guess is that you want to create a class that holds a String for the name and an array of Products (another custom class).
Upvotes: 1