2

Say I have an arraylist a with the values:

a[0] = G
a[1] = B
a[2] = D

I was wondering, how can I create a new arraylist in java, that joins index values at random places and puts it into a new arraylist b So like:

b[0] = GB
b[1] = D

or

b[0] = G
b[1] = BD

or

b[0] = GBD

The order of the values is kept the same, it's just the different combinations of the joins, and at different places over different amounts.

5 Answers 5

1

Something like (pseudocode)

newA = new ArrayList<String>();
for (b : a) {
 if (Math.random() > 0.5) newA.add(b);
 else newA.set(previous, newA.get(previous) + b);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@dantuch @aioobe I thought it was pretty obvious this is pseudocode
0

Assuming you can get the "random position" yourself, my thought would be to use something like the following:

import org.apache.commons.lang.StringUtils;

public List<String> mergeAt(ArrayList<String> input, int offset) {
    List<String> result = new ArrayList<String>();
    result.add(StringUtils.join(input.subList(0, offset), '');
    result.add(StringUtils.join(input.subList(offset, input.size()), '');
    return result;
}

Comments

0
  1. First merge element at index i with index j:

    yourList.set(i, yourList.get(i) + yourList.get(j));
    
  2. Then remove element at index j:

    yourList.remove(j);
    

Comments

0

This satisfies all the conditions you listed above. you can adjust joinIndex and joinSize to whatever you want and it will start at the joinIndex position and concatenate joinSize characters.

Fixed the issue with the exceeding bounds. Now it will just concat as many characters at the end as exist.

import java.util.ArrayList;
import java.util.List;


public class TestJoiner
{
   public static void main(String[] args){

      List<String> list = new ArrayList<String>();
      list.add("G");
      list.add("B");
      list.add("D");
      list.add("L");
      list.add("G");
      list.add("A");

      //Get Random Int
      int joinIndex=3;
      int joinSize=2;

      //check join size, make sure it doesn't exceed bounds.   
      joinSize=(joinIndex+joinSize+1)>list.size()?list.size()-joinIndex-1:joinSize;

      //join
      for(int a=joinIndex;a<joinIndex+joinSize;a++){
         list.set(joinIndex,list.get(joinIndex)+list.get(a+1));
      }         
      //shift
      for(int c=joinIndex+1;c<list.size()-joinSize;c++){
         list.set(c,list.get(c+joinSize));
      }

      //Truncate
      list=list.subList(0,list.size()-joinSize);

      System.out.println(list);
   }
}

1 Comment

congrats on discovering ctrl+shift+f. Your mom must be proud.
0

badger. Here is task solution. Just use List<String> Shaker.shake(List<String>) method.

Shaker class:

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

public class Shaker {
    public static List<String> shake(List<String> sourceList) {
        Random random = new Random();

        // We'll use cloned original list
        LinkedList<String> itemsList = new LinkedList<String>(sourceList);

        // Count how much items need shaking
        int itemsToMerge = itemsList.size();

        // New generated list
        List<String> newList = new ArrayList<String>();

        // Temporary values, used in cycle
        int firstGroupItemIndex = 0;

        while (0 < itemsToMerge) {
            // Select random number of merged items
            int groupLength = random.nextInt(itemsToMerge) + 1;

            // Create inserted string value
            StringBuilder insertedValue = new StringBuilder();
            int lastGroupItemIndex = firstGroupItemIndex + groupLength;
            for (int i = firstGroupItemIndex; i < lastGroupItemIndex; i++) {
                insertedValue.append(itemsList.removeFirst());
            }

            // Add merged string value
            newList.add(insertedValue.toString());
            firstGroupItemIndex = lastGroupItemIndex;

            itemsToMerge -= groupLength;
        }

        return newList;
    }
}

And Test class:

import java.util.List;
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        List<String> originalList = new ArrayList<String>();
        originalList.add("G");
        originalList.add("B");
        originalList.add("C");
        originalList.add("L");
        originalList.add("G");
        originalList.add("A");

        List<String> newList = Shaker.shake(originalList);

        System.out.println("Original list: " + originalList);
        System.out.println("Shaked list: " + newList);
    }
}

Look at the result:

Original list: [G, B, C, L, G, A]
Shaked list: [GBC, LG, A]

If you have questions about the code, I'll answer you with pleasure.
You can always find solution sources at github.com.

2 Comments

The order of the elements should remain the same, even when two are joined, so this is not a correct solution.
Yes, you are right. Thanks! "Correct" problem is easier. Solution updated.

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.