2

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?

5 Answers 5

1

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 });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Please use the Interface List instead of plain Arrays. The interface provides more flexibility and as the question has a PHP background, List is closer to the dynamic nature of PHP, than the C like arrays.
@TilmanPotthof I'm not convinced that lists are the best solution in every case. From what he's shown, he has not exhibited any need for the "dynamic nature" of lists. I'm an advocate of tailoring your solution to the purpose; if he needs the dynamic nature of lists, he can add that. However, if he doesn't need it (and he has not expressed that he does), then it's simpler, cleaner, easier (due to the array initialization), and more safe (the static nature makes sure you (or others) don't mess things up) to use arrays.
You can still take the ArrayList implementation and benefit form it, but taking simple Arrays you have to change every declaration in your code, if your want to switch.
Understood. But the point still stands: I don't believe that lists are the best solution in every case.
I have no use case in mind where a simple Array has an noticeable advantage over an ArrayList. Lists are type-checked and fast, at least in case of ArrayList. But you are right, if you are 100% sure that you will need nothing else but an simple Array, go for it.
1

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).

Comments

1

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);

Comments

0

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.

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.