2

I am looking for a Java (Android) method to add a new element after every n items. The example is as following: I have two different types of items implementing same base class, let's say A and B. What I would like to achieve is to add item from an array (of B elements) to another array (of A elements) after every, let's say, 10 items. I can achieve it by implementing loop logic, but looking for some built-in functionality. Thank for your help.

DETAILS:

ArrayList<A> aList = new ArrayList<A>(Arrays.asList(a, a, a, a, a, a, a));

ArrayList<B> bList = new ArrayList<B>(Arrays.asList(b, b, b);

What I would like to achive is add element from "bList" to "aList" after every 3rd element of "aList" without replacing items in "aList". So my desired ArrayList is like:

ArrayList<A> aList = new ArrayList<A>(Arrays.asList(a, a, a, b, a, a, a, b, a, b));
4
  • 1
    You need to provide more details. For example write pseudocode with expected functionality. Commented Jul 21, 2018 at 20:05
  • 1
    How is this an android question though? Commented Jul 21, 2018 at 20:45
  • Is this school problem, or Yoy have another practical goal? Or this is XY problen Commented Jul 22, 2018 at 15:06
  • @JacekCz Just trying to find more practical solution Commented Jul 22, 2018 at 15:14

3 Answers 3

4

Tested with strings:

    ArrayList<String> aList = new ArrayList<String>(Arrays.asList("a", "a", "a", "a", "a", "a", "a"));

    ArrayList<String> bList = new ArrayList<String>(Arrays.asList("b", "b", "b"));

    int step = 3;

    for (int i = 0; i < bList.size(); i++) {
        int index = (step + 1) * (i + 1) - 1;

        if (index < aList.size()) {
            aList.add(index, bList.get(i));
        } else {
            aList.add(bList.get(i));
        }
    }

    for (String x : aList) {
        System.out.print(x);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Appreciate your effort. Actually, I'm able to achieve it with loop, looking for something built-in like sort in Collections. "I can achieve it by implementing loop logic, but looking for some built-in functionality"
@FARID why? You can't achieve that with sorting, how would you even write the comparator for that?
I'm not saying using sort. I just referenced sort as a built-in method so we don't have to manually sort any ArrayList. I just gave it as an example. So, I am just looking for some built-in method to achieve what I asked in the question. Didn't have high expectation of the existence of such method, but thought maybe there is something like that
0

I don't believe such a method exists. You could use a different approach, like:

Creating an inner class that extends the ArrayList class and overriding the add() method to change an inner global variable (so that you don't declare it final), Declare a method called "specialAdd" or something and make it return a Boolean It will have this line of code at the bottom: return super.add(o); and the method should also increment the global variable you defined earlier and when if it equals three you will add the desired element from bList to aList.

Comments

-2

If I understood, you have a common class : BaseClass, and two class that extends Baseclass, ClassA and ClassB. To do such array, just create an array of type Baseclass : ArrayList<BaseClass> and write a static method like you said to loop (I do not think there is a built-in method, but not sure) and add ClassA then ClassB, or if you have an array of A type, you can add it using .addAll() method.

I cannot seem to find a way without looping.

1 Comment

Actually your answer is kinda re-iteration of my question :))

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.