-1

I need help with #3 and #5.

  1. Create a collection and fill it with 20 random numbers between 1 and 15 (inclusive)

    List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1),
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1),
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1),
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1)
       ));
    
  2. Print the numbers

    System.out.println(list1);
    
  3. Seed the constructor of class Random with the value 13.

    • what does it mean to seed the ctor?
  4. Serialize the collection to a file called Numbers.ser

    try (ObjectOutputStream serialize = new ObjectOutputStream(new FileOutputStream("src/serialize/Numbers.ser"))) {
        serialize.writeObject(list1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  5. Deserialize the file into a new collection called numberFromFile. Remove all duplicate numbers

    • This is where I am confused on how to deserialize. What is the explanation?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • #3: https://stackoverflow.com/questions/12458383/java-random-numbers-using-a-seed – achAmháin Apr 28 '18 at 16:31
  • #5: https://stackoverflow.com/questions/21520337/de-serialize-a-file-and-return-an-arraylist-containing-the-objects-in-the-file – achAmháin Apr 28 '18 at 16:32
  • Possible duplicate of [Java serialization of multidimensional array](https://stackoverflow.com/questions/1467193/java-serialization-of-multidimensional-array) – Adrian Apr 28 '18 at 21:59

2 Answers2

0

Initialising Random with a seed means it will generate the same sequence of random numbers again. Just check the docs: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

For reading distinct numbers from your file you could do something like:

try(ObjectInputStream deserialize = new ObjectInputStream(new FileInputStream("src/serialize/Numbers.ser"))) {
    List<Integer> list2 = (List<Integer>) deserialize.readObject();
    System.out.println(list2.stream().distinct().collect(Collectors.toList()));
} catch(...) { ... }
Dennis Hunziker
  • 1,293
  • 10
  • 19
0
//Seed the constructor of class Random with the value 13.
Random rand = new Random(13);

//Create a collection and fill it with 20 random numbers between 1 and 15 (inclusive)

List<Integer> list1 = new ArrayList<>();

for (int i = 0; i < 20; i++) {
    list1.add(rand.nextInt(15) + 1);  // Note here
}

//Print the numbers

System.out.println(list1);

//Serialize the collection to a file called Numbers.ser

try (ObjectOutputStream out = new ObjectOutputStream(
        new FileOutputStream("Numbers.ser"))) {
    out.writeObject(list1);
} catch (IOException e) {
    e.printStackTrace();
}

//Deserialize the file into a new collection called numberFromFile. Remove all duplicate numbers
//this is where I am confused on how to deserialize. Can you someone explain this to me?
List<Integer> numberFromFile = null;
try (ObjectInputStream in = new ObjectInputStream(
        new FileInputStream("Numbers.ser"))) {
    numberFromFile = (List<Integer>) in.readObject();
} catch (Exception e) {
    e.printStackTrace();
}

//System.out.println(numberFromFile);

Set<Integer> distinct = new HashSet<>(numberFromFile); 
System.out.println(distinct);
rifaqat
  • 301
  • 2
  • 8