[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Java

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 1

File: javaman.png (59KB, 549x1024px) Image search: [Google]
javaman.png
59KB, 549x1024px
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
Thread posts: 16
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.