M
M
Max28888882019-05-30 14:00:35
Java
Max2888888, 2019-05-30 14:00:35

How to get 5 random values ​​from an array without repetitions and write them into one variable? [java]?

It is necessary to get 5 random values ​​​​from the array and write them into one variable of the String type, tell me how this can be implemented so that there are no duplicates, I throw off my code for an example, do not laugh at it)

public class Test {
  public static void main(String[] args) {
    String[] shops = {"DE", "CH", "AT", "NL", "FR", "DK", "ES", "IT", "EN", "SE", "NO", "FI", "PT", "BE", "LU", "RO", "GR", "CZ", "PL", "HU"};

    String shopsForTest = (shops[new Random().nextInt(shops.length)]) + "+" + (shops[new Random().nextInt(shops.length)]) + "+" + (shops[new Random().nextInt(shops.length)]) +
            "+" + (shops[new Random().nextInt(shops.length)]) + "+" + (shops[new Random().nextInt(shops.length)]);
    System.out.println(shopsForTest);
      }

5cefb85df090b019495480.jpeg

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
Nick Smith, 2019-05-30
@Max2888888

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

class Scratch {
    public static void main(String[] args) {
        String[] shops = {"DE", "CH", "AT", "NL", "FR", "DK", "ES", "IT", "EN", "SE", "NO", "FI", "PT", "BE", "LU", "RO", "GR", "CZ", "PL", "HU"};

        List<String> shopList = Arrays.asList(shops);
        Collections.shuffle(shopList);
        String result = shopList.stream().limit(5).collect(Collectors.joining("+"));
        System.out.println(result);
    }
}

U
UNy, 2019-05-30
@UNy

You make a loop with 5 iterations, randomly after receiving the value, remove it from the array (use array list) and add it to the string builder.

T
TheRonCronix, 2019-05-30
@TheRonCronix

In a loop from 1 to 5:
{
Pick an index with a random value from 0 to shops.length-1.
Pick a value from shops at that index and save/output it wherever you want.
Delete the selected item from shops.
}

A
Anton, 2019-05-30
@Phobos911

String[] shops = { "DE", "CH", "AT", "NL", "FR", "DK", "ES", "IT", "EN", "SE", "NO", "FI", "PT", "BE", "LU",
        "RO", "GR", "CZ", "PL", "HU" };
    List<String> list =  new ArrayList<String>(Arrays.asList(shops));
    StringBuilder builder = new StringBuilder();
    int count =0;
    while (count < 5) {
      Random r = new Random();
      int rand = r.nextInt(list.size());
      builder.append(list.get(rand)).append(" ");
      list.remove(rand);
      count++;
    }
    String shopsForTest = builder.toString();
    System.out.println(shopsForTest);

M
Max2888888, 2019-05-30
@Max2888888

TheRonCronix UNy
Changed the array to a list, created a loop with 5 iterations, randomized the values ​​from the list, figured out how to delete by index and hung up. Already closer to the solution, but I can not figure out how to use the "string builder". That is, how can I write a new value to the String variable from each iteration of the loop without erasing the previous one?

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

    StringBuilder builder = new StringBuilder();

    List shops = new ArrayList();
        shops.add("DE");
        shops.add("CH");
        shops.add("AT");
        shops.add("NL");
        shops.add("FR");
        shops.add("DK");
        shops.add("ES");
        shops.add("IT");
        shops.add("EN");
        shops.add("SE");
        shops.add("NO");
        shops.add("FI");
        shops.add("PT");
        shops.add("BE");

    int index = 0;
    for (int x = 0; x < 5; x++) {
      Random random = new Random();
      index = random.nextInt(shops.size());
      System.out.println(shops.get(index));

//      shops.remove(index);
    }

    String shopsForTest = shops.get(index) + "+";
    System.out.println(shopsForTest);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question