multiply string element per int element from two arrays Java
Question: I am learning Java and looking for a comprehensive code of multiplying the
elements from 2 arrays, possibly without importing anything to achieve it.

In Python it's quite easy:

    
    
    a=['a','b','c','d']
    b=[1,2,3,4]
    [x*y for x,y in zip(a,b)]
    ['a', 'bb', 'ccc', 'dddd']
    

How can I achieve the same thing in Java, when the first array is an array of
strings and the second is integers?


Answer: I'm afraid Java isn't going to support this kind of thing natively, and you'll
need to perform some of your own logic to implement it. Let's say you've got
your `String[]`..

    
    
    String[] a = {"a", "b", "c", "d"};
    

And you've got your `int[]`..

    
    
    int[] b = {1,2,3,4};
    

Next, you'll need to check that the arrays are the same size.

    
    
    if(a.length == b.length) {
        // Continue.
    }
    

Then you need to implement a loop, to go through each item in the arrays.

    
    
    for(int x = 0; x < a.length; x++)
    {
        // Some looping code.
    }
    

And you're going to grab each item.

    
    
    String value = a[x];
    
    int multiplier = b[x];
    

If you're not importing anything, you declare the total value:

    
    
    String total = "";
    

But if you're allowing for a `StringBuilder`, then you'll import it and
declare..

    
    
    StringBuilder total = new StringBuilder();
    

_**NOTE:_** StringBuilder is strongly recommended here.

And then you're looping `multiplier` amount of times..

    
    
    for(int y = 0; y < multiplier; y++)
    {
        // If you use StringBuilder..
        total.append(value);
    
        // If you don't..
        total += value;
    }
    
    // If you use StringBuilder..
    a[x] = total.toString();
    
    // If you don't...
    a[x] = total;
    

This will set the value of `a[x]` to the repeated `String`.

_**NOTE_** : Something that's also important is leaning good practise. If
you're using Java code, it's considered terrible practise to repeatedly
concatenate `String` objects. `StringBuilder` is more efficient, and is the
Java standard. I would strongly recommend using this.

Have fun putting it all together!!

