avoid using java streams- android - java

I have a code but I want to avoid using java streams because streams are not supported in android. Here is my code:
import java.util.Arrays;
import static java.util.Arrays.stream;
import java.util.concurrent.*;
public class VogelsApproximationMethod {
final static int[] demand = {30, 20, 70, 30, 60};
final static int[] supply = {50, 60, 50, 50};
final static int[][] costs = {{16, 16, 13, 22, 17}, {14, 14, 13, 19, 15},
{19, 19, 20, 23, 50}, {50, 12, 50, 15, 11}};
final static int nRows = supply.length;
final static int nCols = demand.length;
static boolean[] rowDone = new boolean[nRows];
static boolean[] colDone = new boolean[nCols];
static int[][] result = new int[nRows][nCols];
static ExecutorService es = Executors.newFixedThreadPool(2);
public static void main(String[] args) throws Exception {
int supplyLeft = stream(supply).sum();
int totalCost = 0;
while (supplyLeft > 0) {
int[] cell = nextCell();
int r = cell[0];
int c = cell[1];
int quantity = Math.min(demand[c], supply[r]);
demand[c] -= quantity;
if (demand[c] == 0)
colDone[c] = true;
supply[r] -= quantity;
if (supply[r] == 0)
rowDone[r] = true;
result[r][c] = quantity;
supplyLeft -= quantity;
totalCost += quantity * costs[r][c];
}
stream(result).forEach(a -> System.out.println(Arrays.toString(a)));
System.out.println("Total cost: " + totalCost);
es.shutdown();
}
i would be grateful if anyone can help me with this because i can't understand how stream works.

Well that is quite easy, you can use for loop as Mike M suggested in his comment see examples below:
int supplyLeft = 0;
int[] supply = {50, 60, 50, 50};
for (int i : supply) {
supplyLeft += i;
}
System.out.println(supplyLeft);
to replace
int supplyLeft = stream(supply).sum();
if you don't want foreach either then
int supplyLeft = 0;
for (int i = 0; i < supply.length; i++) {
supplyLeft += supply[i];
}
System.out.println(sum);
As far as iterating through 2D array and replacing the java-8 stream as shown below is concerned
stream(result).forEach(a -> System.out.println(Arrays.toString(a)));
you can use a for loop or Arrays.deepToString() as per your needs see below example:
//assume your array is below, you can replace it with results everywhere
int[][] costs= { { 16, 16, 13, 22, 17 }, { 14, 14, 13, 19, 15 },
{ 19, 19, 20, 23, 50 }, { 50, 12, 50, 15, 11 } };
for (int i = 0; i < costs.length; i++) {
System.out.println(Arrays.toString(costs[i]));
}
Above for loop will print you the array in the following manner
[16, 16, 13, 22, 17]
[14, 14, 13, 19, 15]
[19, 19, 20, 23, 50]
[50, 12, 50, 15, 11]
If you use Arrays.deepToString(costs) then you get an output as shown below:
[[16, 16, 13, 22, 17], [14, 14, 13, 19, 15], [19, 19, 20, 23, 50], [50, 12, 50, 15, 11]]
Above to replace

Related

how sum of 2 arraylist?

i got below mentioned error when run code:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.LinkedList.checkPositionIndex(Unknown Source)
at java.util.LinkedList.addAll(Unknown Source)
at Collection.Dynamycmaasiv.Collecktionaddlist.main(Collecktionaddlist.java:36)
code
public static void main(String[] args) {
LinkedList<Integer> num = new LinkedList<Integer>();
LinkedList<Integer> numodd = new LinkedList<Integer>();
LinkedList<Integer> numeven = new LinkedList<Integer>();
LinkedList<Integer> sumoffevenandodd = new LinkedList<Integer>();// help
// me
// to
// solve
for (double i = 0; i < 50; i++) {
num.add((int) i);
if (i % 2 == 0) {
numeven.add((int) i);
} else {
numodd.add((int) i);
}
}
System.out.println(num);
System.out.println("-----------------");
System.out.println(numodd);
System.out.println("-----------------");
System.out.println(numeven);
for (int i =0; i<numeven.size(); i++){
sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null);
}
System.out.println(sumoffevenandodd);
}
}
addAll() is not about adding up numbers. It is about adding all the elements of the method parameter to the collection itself.
So, you need to loop, like
int sum = 0;
for (Integer numberFromList : numeven) {
sum = sum + numberFromList;
Or, if you have Java8, you can use streams:
int sumEven = numeven.stream().sum();
Sum, done.
And for the record: the real lesson to be learned here: read the javadoc. Don't assume that method called addAll() does what you suppose it does. Turn to the javadoc and inform yourself what reality thinks about your assumptions.
But just to be clear; as I got carried away with your question, too.
In your code, if you change
sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null);
to
sumoffevenandodd.add(numeven.get(i)+ numodd.get(i));
it should work, too.
Long story short: if you intended to really have a list with 50 sums within, then my first paragraphs do not really help with your problem.
But it isn't exactly clear what you wanted to do; so I leave my answer as is - to address both possible explanations what is "wrong" in your logic.
if the intention of the question is
num odd
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
num even
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
sum of odd and even
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97]
then
for (int i =0; i< numeven.size(); i++){
sumoffevenandodd.add(numeven.get(i)+ numodd.get(i));
}

How to fill an array with smaller arrays in a random way?

I've got an array with 225 elements and 15 smaller arrays which their length sum is exactly 225.
The point is that I need to fill the larger array with these smaller arrays but in a random way.
private final short datosdeNivel[]= new short[225];
private final short diecinueve[]= {19, 19};
private final short veintiseis[]= {26, 26, 26};
private final short dieciocho[]= {18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18};
private final short veintidos[]= {22, 22};
private final short veintiuno[]={21, 21, 21, 21, 21, 21, 21, 21, 21, 21};
private final short cero[]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
private final short diecisiete[]= {17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
private final short dieciseis[]= {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
private final short veinte[]= {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
private final short veinticuatro[]= {24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24};
private final short veinticinco[]= {25, 25, 25, 25};
private final short veintiocho[]= {28, 28};
private final short uno[]= {1, 1, 1, 1, 1, 1, 1};
private final short nueve[]= {1};
private final short ocho[]= {9, 9, 9, 9, 9, 9, 9, 9, 9};
How can I establish a random order so every time the program runs the order in which the smaller arrays are placed in the larger array is different?
This would be a way to fill it in order:
int aux;
aux= diecinueve.length;
for(int i=0; i<diecinueve.length; i++)
{
datosdeNivel[i]= diecinueve[i];
}
for(int i=0; i<veintiseis.length; i++)
{
datosdeNivel[aux]= veintiseis[i];
aux++;
}
for(int i=0; i<dieciocho.length; i++)
{
datosdeNivel[aux]= dieciocho[i];
aux++;
}
for(int i=0; i<veintidos.length; i++)
{
datosdeNivel[aux]= veintidos[i];
aux++;
}
for(int i=0; i<veintiuno.length; i++)
{
datosdeNivel[aux]= veintiuno[i];
aux++;
}
for(int i=0; i<cero.length; i++)
{
datosdeNivel[aux]= cero[i];
aux++;
}
for(int i=0; i<diecisiete.length; i++)
{
datosdeNivel[aux]= diecisiete[i];
aux++;
}
for(int i=0; i<dieciseis.length; i++)
{
datosdeNivel[aux]= dieciseis[i];
aux++;
}
for(int i=0; i<veinte.length; i++)
{
datosdeNivel[aux]= veinte[i];
aux++;
}
for(int i=0; i<veinticuatro.length; i++)
{
datosdeNivel[aux]= veinticuatro[i];
aux++;
}
for(int i=0; i<veinticinco.length; i++)
{
datosdeNivel[aux]= veinticinco[i];
aux++;
}
for(int i=0; i<veintiocho.length; i++)
{
datosdeNivel[aux]= veintiocho[i];
aux++;
}
for(int i=0; i<uno.length; i++)
{
datosdeNivel[aux]= uno[i];
aux++;
}
for(int i=0; i<nueve.length; i++)
{
datosdeNivel[aux]= nueve[i];
aux++;
}
for(int i=0; i<ocho.length; i++)
{
datosdeNivel[aux]= ocho[i];
aux++;
}
You could try creating an ArrayList, containing all of your smaller arrays:
ArrayList<short[]> arrays = new ArrayList<>();
arrays.add(ocho);
arrays.add(veintiocho);
// ...
and then access indexes randomly, removing from the list each time you add to your large array:
Random rand = new Random();
while (!arrays.isEmpty()) {
int index = rand.nextInt(array.size());
short[] s = arrays.get(index);
for(int i = 0; i < s.length; i++)
{
datosdeNivel[aux]= s[i];
aux++;
}
arrays.remove(index);
}
This will add each array to the large array in a random order.
In pseudo code:
Put the arrays in a List
Shuffle the list using Collections.shuffle(list)
iterate over the list, filling the final array with elements of each array
If I were you, I'll give each array an index, then make a bucket of these indexes, then randomly pick up an index from the bucket, do your copy job, and remove this used index from your bucket.

Looping through TextView and setting the size

I'm trying to loop through a specified list of sizes and change the size of the text in my TextView accordingly. How can I go about doing this properly?
public void TextBigger(View view) {
int[] textViewSizes = new int[] {10, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
for(int i=0; i < textViewSizes.length; i++) {
text_View.setTextSize(TypedValue.COMPLEX_UNIT_SP, i);
}
}
//make these class scope
int currentTextSize = 0;
int[] textViewSizes = new int[] {10, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
public void TextBigger(View view) {
if(currentTextSize < textViewSizes.length - 1)
currentTextSize++;
text_View.setTextSize(TypedValue.COMPLEX_UNIT_SP, textViewSizes[currentTextSize]);
}

is the check broken because of the array or methods im using?

Don't mind the long code the column methods are repetitive, anyways, the assignment below is bingo and i have put together a check for the first player (player two is essentially copy and paste),and i can't seem to figure out why my check isn't working. Is it the 2D array, or the methods? could someone please explain to me in detail what's the problem and what i need to do to fix it?
package bingo;
import static java.rmi.Naming.list;
import java.util.ArrayList;
import java.util.Collections;
import static java.util.Collections.list;
import java.util.List;
import java.util.Scanner;
public class Bingo {
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
int[] random = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75};
int x = 0, get = 0, gameCount = 0, gameCount2 = 0;
boolean turn = true, gameOver = true;
int[][] cardofzeros = new int[5][5];
int[] column1 = new int[5];
int[] column2 = new int[5];
int[] column3 = new int[5];
int[] column4 = new int[5];
int[] column5 = new int[5];
int[][] card1 = new int[][]{column1(column1), column2(column2), column3(column3), column4(column4), column5(column5)};
System.out.println("");
do {
System.out.println("Enter a positive integer to begin BINGO!/GO AGAIN");
get = input.nextInt();
if (get > 0) {
do {
List l = new ArrayList();
for (int i : random) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 2; i++) {
System.out.println(l.get(i));
x = i;
}
System.out.println(" ");
for (int i = 0; i < card1.length; i++) {
for (int j = 0; j < card1.length; j++) {
cardofzeros(cardofzeros, card1, x, j, i, turn,gameCount);
turn=false;
}
}
//System.out.println(random);
} while (turn == true);
} else {
}
} while (gameCount < 5 || gameCount2 < 5);
if (gameCount == 5) {
System.out.println("PLAYER 1 WINS");
} else {
System.out.println("PLAYER 2 WINS");
}
}
public static int[] column1(int[] column1) {
int[] colm = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
List l = new ArrayList();
for (int i : colm) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column1;
}
public static int[] column2(int[] column2) {
int[] colm2 = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
List l = new ArrayList();
for (int i : colm2) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column2;
}
public static int[] column3(int[] column3) {
int[] colm3 = {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};
List l = new ArrayList();
for (int i : colm3) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column3;
}
public static int[] column4(int[] column4) {
int[] colm4 = {46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60};
List l = new ArrayList();
for (int i : colm4) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column4;
}
public static int[] column5(int[] column5) {
int[] colm2 = {61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75};
List l = new ArrayList();
for (int i : colm2) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column5;
}
public static int cardofzeros(int[][] cardofzeros, int[][] card1, int x, int j, int i, boolean turn,int gameCount) {
if (card1[i][j] == x) {
cardofzeros[i][j] = 1;
System.out.println(x + "filled");
turn = false;
} else {
turn = false;
}
horzcheck(card1, cardofzeros, gameCount);
return gameCount;
}
public static int numBingo(int x) {
return x;
}
public static int horzcheck(int[][] card1, int[][] cardofzeros, int gameCount) {
for (int j = 0; j < card1.length; j++) {
for (int i = 0; i < card1.length; i++) {
if (cardofzeros[i][j] == 1) {
gameCount++;
} else {
}
}
}
return gameCount;
}
}

A abnormally working java loop,

This is a brute force attempt to solve the problem, but it is not giving the right answer. The program runs, but its not producing desired output. I believe the logic and program is correct.
This is problem of a famous site (don't want to a spoiler)
It asks for the number that produces the longest Collatz chain under one million.
class Euler
{
public static void main (String args[])
{
long len,longLength=0;
for(long i =3;i<=1000000;i++)
{
len = Euler14.numFucs(i);
System.out.println("Ans"+len+"\t"+i);
if(len>longLength)
longLength=len;
}
System.out.println(longLength);
}
public static long numFucs(long num)
{
long count=1,$test=0;
while(num>1)
{
if(num%2==0)
{
num=num/2;
}
else
{
num=3*num+1;
}
count++;
}
//System.out.println("\tEnd");
return count;
}
}
Well, I feel like it'd be cheating to give you the code for the right answer, as a fellow Project Euler fan. You're outputting the length of the longest chain, not the number that obtains it. If you really want, I can show you the one line that needs to change, but, honestly, this is a simple fix, and I challenge you to do it yourself.
If the program is suppose to compute the number of steps in the Collatz Conjecture the implementation looks fine to me.
The sequence of numbers in described by OEIS Sequence A008908.
Here's is your program together with some debug output.
class Test {
public static void main(String args[]) {
long len, longLength = 0;
System.out.println(Test.numFucs(13));
String[] correct = ("1, 1, 2, 8, 3, 6, 9, 17, 4, 20, 7, 15, 10, 10, 18,"
+ " 18, 5, 13, 21, 21, 8, 8, 16, 16, 11, 24, 11, 112, "
+ "19, 19, 19, 107, 6, 27, 14, 14, 22, 22, 22, 35, 9, "
+ "110, 9, 30, 17, 17, 17, 105, 12, 25, 25, 25, 12, "
+ "12, 113, 113, 20, 33, 20, 33, 20, 20, 108, 108, 7,"
+ " 28, 28, 28, 15, 15, 15, 103").split(", ");
for (int i = 0; i <= 70; i++) {
len = Test.numFucs(i);
System.out.printf("i = %2d, Correct %3s, Computed: %3d%n", i,
correct[i], len);
if (len > longLength)
longLength = len;
}
System.out.println(longLength);
}
public static long numFucs(long num) {
long count = 1;
while (num > 1) {
if (num % 2 == 0) {
num = num / 2;
} else {
num = 3 * num + 1;
}
count++;
}
// System.out.println(count);
return count;
}
}
Output:
i = 0, Correct 1, Computed: 1
i = 1, Correct 1, Computed: 1
i = 2, Correct 2, Computed: 2
i = 3, Correct 8, Computed: 8
i = 4, Correct 3, Computed: 3
i = 5, Correct 6, Computed: 6
i = 6, Correct 9, Computed: 9
i = 7, Correct 17, Computed: 17
i = 8, Correct 4, Computed: 4
i = 9, Correct 20, Computed: 20
i = 10, Correct 7, Computed: 7
i = 11, Correct 15, Computed: 15
i = 12, Correct 10, Computed: 10
i = 13, Correct 10, Computed: 10
i = 14, Correct 18, Computed: 18
i = 15, Correct 18, Computed: 18
i = 16, Correct 5, Computed: 5
...
As you can see it follows the OEIS sequence.
The error must be some where else.

Categories