Could anyone help me with the following:
"Make a java program that generates random sequences of integers, where the arrays increase in size (this means multiple arrays). "
>>59255246
Have you tried installing gentoo?
>>59255246
Have you tried thinking?
>>59255246
Have you tried pooing in a loo?
>sequences of integers
>mentions arrays out of nowhere
who wrote this?
import java.util.Random;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class HelloWorld {
public static void main(String[] args){
NumberFormat formatter = new DecimalFormat("###");
int arraysNr = 7;
if(args.length > 0) {
arraysNr = Integer.parseInt(args[0]);
}
for(int i=1; i<=arraysNr; i++) {
int[] array = new int[i];
for(int j=0; j<array.length; j++) {
array[j] = randInt(1, 100);
}
for(int j=0; j<array.length; j++) {
System.out.print(String.format("%3s", Integer.toString(array[j])) + " ");
}
System.out.println();
}
}
public static int randInt(int min, int max) {
Random rand = new Random();
return min + rand.nextInt(max - min + 1);
}
}
>>59256638
>making a new Random for every int you generate
>>59256688
Good catch, it's better to make it final static.
>>59256638
this is what i would write if i wanted the kid to get caught copy-pasting
>not using recursing
>>59256753
are you referring to the class name
>>59256638
overengineered as fuck, jesus, do you even jdk6+?
(assuming int start and end are parameters, since OP doesn't say HOW the desired size is specified or if it's even done in a single call)
Random random = new Random();
return IntStream.range(start, end)
.mapToObj(x -> new ArrayList<Integer>(x))
.map(x => {x.replaceAll(random::nextInt); return x.toArray();})
.collect(Collectors.toList()); // list of arrays
>>59256979
probably needs a call to a fill method first since replaceAll only replaces present values and the constructor only sets the capacity
or just start off with an int[x] that defaults to all 0 iiirc, then turn that into a list and relaceAll
>>59256979
>>59257017
Actually tested answer:
Random random = new Random();
return IntStream.range(4, 20)
.mapToObj(Integer[]::new)
.map(Arrays::asList)
.map(x -> {x.replaceAll(random::nextInt); return x.toArray();})
.collect(Collectors.toList());
>>59257127
Ok this time I've actually run and debugged it, and found a list of random ints of increasing length
Random random = new Random();
Object x = IntStream.range(4, 20)
.mapToObj(Integer[]::new)
.map(y -> {Arrays.fill(y, 0); return y;})
.map(Arrays::asList)
.map(y -> {y.replaceAll(z -> random.nextInt()); return y.toArray();})
.collect(Collectors.toList());
inadvertent parameter binding in lambdas can really kill things
>>59257217
And this is what you get when you stop doing patchworks and start over knowing the pitfalls this time
Random random = new Random();
Object x = IntStream.range(4, 20)
.mapToObj(int[]::new)
.map(y -> Arrays.stream(y).map(z -> random.nextInt()).toArray())
.collect(Collectors.toList());
as good as it gets now