How can i display array to show integer? - java

I want to display something like this:
1 {1,2,3,4,5,6,7,8,9}
2 {1,2,3,4,5,6,7,8,9}
...
9 {1,2,3,4,5,6,7,8,9}
Nothing works
int[][] areas = new int[9][9];
for(int j=0; j<=8; j++)
{ // do i need to initialize here first array? something like areas [j][] = j ;
// how can i do that?
for(int i=0; i<=8; i++)
{
areas[j][i] = i;
Log.d("tiles", String.valueOf(areas[j][i])); // here i get from 1 to 9, 9 times
}
Log.d("area", String.valueOf(areas[j]));
//here is the problem
//i want to show 1 {for all tiles for this 1 area}, than 2 {for all tiles for
//this 2 area}... for each but it only display an address
}
I want to have control when I want to go to area 6 to tile 5 for example

Try Arrays.toString
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String s = Arrays.toString(arr).replace("[", "{").replace("]", "}");
for (int i = 1; i <= arr.length; i++) {
System.out.println(i + " " + s);
}
Ouput
1 {1, 2, 3, 4, 5, 6, 7, 8, 9}
2 {1, 2, 3, 4, 5, 6, 7, 8, 9}
3 {1, 2, 3, 4, 5, 6, 7, 8, 9}
4 {1, 2, 3, 4, 5, 6, 7, 8, 9}
5 {1, 2, 3, 4, 5, 6, 7, 8, 9}
6 {1, 2, 3, 4, 5, 6, 7, 8, 9}
7 {1, 2, 3, 4, 5, 6, 7, 8, 9}
8 {1, 2, 3, 4, 5, 6, 7, 8, 9}
9 {1, 2, 3, 4, 5, 6, 7, 8, 9}

int[][] areas = new int[10][10];
for (int j = 0; j <= 8; j++) {
System.out.print(j + 1);
System.out.print("{");
for (int i = 0; i <= 8; i++) {
areas[j][i] = i;
if (areas[j][i] < 8) {
System.out.print(String.valueOf(areas[j][i] + 1) + ","); // here i get from 1 to 9, 9 times
} else {
System.out.print(String.valueOf(areas[j][i] + 1));
}
}
System.out.print("}");
}
OUTPUT
1{1,2,3,4,5,6,7,8,9}2{1,2,3,4,5,6,7,8,9}3{1,2,3,4,5,6,7,8,9}4{1,2,3,4,5,6,7,8,9}5{1,2,3,4,5,6,7,8,9}6{1,2,3,4,5,6,7,8,9}7{1,2,3,4,5,6,7,8,9}8{1,2,3,4,5,6,7,8,9}9{1,2,3,4,5,6,7,8,9}

Related

how to add values in an array from last value to first

How can I add the values in the arrNumbers that exceed 6 to add to a new array starting from the last value and ending at the first.
This is what I have written, but does not produce the right output.
int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2 };
int[] newArrNumbers = new int[6];
for(int i = 0; i < arrNumbers.length ; i++){
newArrNumbers[i % 6] += arrNumbers[i];
}
The actual output:
newArrNumbers = [2, 4, 3, 4, 5, 6]
However, I want the output to ADD to the LAST VALUE in the arrNumbers, going from right to left, not left to right. So result should be:
newArrNumbers = [1, 2, 3, 4, 7, 7]
Try this.
int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2 };
int[] newArrNumbers = new int[6];
for(int i = 0; i < arrNumbers.length ; i++){
newArrNumbers[i < 6 ? i : (6 - (i % 6) - 1)] += arrNumbers[i];
}
System.out.println(Arrays.toString(newArrNumbers));
output:
[1, 2, 3, 4, 7, 7]

My selection sorting code is failing on one index

I'm having great issues with my selection sort code can anyone explain to me where i'm going wrong?
this code works for the most part however when it gets to the 4/5th round it false to sort the 5 as the next lowest instead going to the 6 in the array.
this is what my output looks like. As you can see the 5 is clearly in the wrong place.
any help understand why this is would be great thank you.
[1, 9, 4, 10, 5, 3, 6, 2, 8, 7]
[1, 2, 9, 10, 5, 3, 6, 4, 8, 7]
[1, 2, 3, 10, 9, 5, 6, 4, 8, 7]
[1, 2, 3, 4, 10, 9, 5, 6, 8, 7]
[1, 2, 3, 4, 6, 10, 9, 5, 8, 7]
[1, 2, 3, 4, 6, 5, 10, 9, 8, 7]
[1, 2, 3, 4, 6, 5, 7, 10, 9, 8]
[1, 2, 3, 4, 6, 5, 7, 8, 10, 9]
[1, 2, 3, 4, 6, 5, 7, 8, 9, 10]
-
public class Selection {
void findSmallestNumberIndex(int[] numbers, int index) {
//int[] numbers = {4, 9, 2, 10, 5, 3, 6, 1, 8, 7};
int n = numbers.length;
int min_idx = index; //4
for (int j = index + 1; j < n; j++) {
if (numbers[j] < numbers[min_idx]) {
min_idx = j;
}
int temp = numbers[min_idx];
numbers[min_idx] = numbers[index];
numbers[index] = temp;
}
}
public static void main(String[] args) {
int[] numbers = {4,9,2,10,5,3,6,1,8,7};
int NumLen = numbers.length;
int[] sortedNum = new int[NumLen];
int index;
index = 0;
Selection OB = new Selection();
do {
OB.findSmallestNumberIndex(numbers, index);
index++;
System.out.println(Arrays.toString(numbers));
} while (index != (NumLen - 1));
}
}
Move the logic to adjust array outside the for loop, here's how it should look:
void findSmallestNumberIndex(int[] numbers, int index) {
//int[] numbers = {4, 9, 2, 10, 5, 3, 6, 1, 8, 7};
int n = numbers.length;
int min_idx = index; //4
for (int j = index + 1; j < n; j++) {
if (numbers[j] < numbers[min_idx]) {
min_idx = j;
}
}
int temp = numbers[min_idx];
numbers[min_idx] = numbers[index];
numbers[index] = temp;
}

How to add an Employee Number to my print statement?

import java.util.*;
import java.util.Arrays;
public class EmployeeWeeklyHours{
public static void main(String[] args) {
int[][] employeeHours= new int[][] {
{ 2, 4, 3, 4, 5, 8, 8},
{ 7, 3, 4, 3, 3, 4, 4},
{ 3, 3, 4, 3, 3, 2, 2},
{ 9, 3, 4, 7, 3, 4, 1},
{ 3, 5, 4, 3, 6, 3, 8},
{ 3, 4, 4, 6, 3, 4, 4},
{ 3, 7, 4, 8, 3, 8, 4},
{ 6, 3, 5, 9, 2, 7, 9}};
int [] finalHours = new int[8];
for (int i = 0; i < finalHours.length; i++) {
int total = 0;
for (int j = 0; j < finalHours.length - 1; j++) {
total += employeeHours[i][j];
finalHours[i] = total;
}
}
java.util.Arrays.sort(finalHours);
int[] sort = new int[finalHours.length];
for (int i = 0; i < finalHours.length; i++)
sort[i] = finalHours[i];
for (int i = 7; i > -1; i--)
System.out.println(sort[i]);
}
}
Employee 0: 2 4 3 4 5 8 8
Employee 1: 7 3 4 3 3 4 4
Employee 2: 3 3 4 3 3 2 2
Employee 3: 9 3 4 7 3 4 1
Employee 4: 3 5 4 3 6 3 8
Employee 5: 3 4 4 6 3 4 4
Employee 6: 3 7 4 8 3 8 4
Employee 7: 6 3 5 9 2 7 9
As you can see from my code, I am supposed to list the total hours worked for each employee in descending order. However, I seem to be stumped on a way to show the Employee Numbers next to their total hours.
E.G. "Employee 7 worked 42 hours.
Is there any way that I can list the Employee Numbers alongside the sorted numbers without hard coding them in? I feel like there is a simple answer to my question, but nothing comes to mind right now.
One solution would be using a TreeMap since it sorts on the keys automatically.
TreeMap<Integer, Integer> treeMap =
new TreeMap<Integer, Integer>(Collections.reverseOrder());
for (int i = 0; i < finalHours.length; i++) {
treeMap.put(finalHours[i], i);
}
for (Entry<Integer, Integer> entry : treeMap.entrySet()) {
System.out.println( "Employee " + entry.getValue() + " worked " + entry.getKey() + " hours.");
}
for (int i=0; i < finalHours.length; i++) {
System.out.println(String.format("Employee %d worked %d hours.", i, finalHours[i]));
}
And since you mentioned hardcoding, when you write a loop, rather than putting a magic "7" in there, use variables like finalHours.length or employeeHours.length. The same principle holds in general: refer to variables even when you "know" what the value is going to be.
if you want this code ?
import java.util.*;
import java.util.Arrays;
public class EmployeeWeeklyHours{
public static void main(String[] args) {
int[][] employeeHours= new int[][]{
{ 2, 4, 3, 4, 5, 8, 8},
{ 7, 3, 4, 3, 3, 4, 4},
{ 3, 3, 4, 3, 3, 2, 2},
{ 9, 3, 4, 7, 3, 4, 1},
{ 3, 5, 4, 3, 6, 3, 8},
{ 3, 4, 4, 6, 3, 4, 4},
{ 3, 7, 4, 8, 3, 8, 4},
{ 6, 3, 5, 9, 2, 7, 9}};
String [] finalHours = new String[8];
for (int i = 0; i < finalHours.length; i++)
{
int total = 0;
for (int j = 0; j < finalHours.length-1; j++)
{
total += employeeHours[i][j];
finalHours[i] = "Employee "+ i+" worked "+total + "hours";
}
}
java.util.Arrays.sort(finalHours);
String[] sort = new String[finalHours.length];
for (int i = 0; i < finalHours.length; i++)
{
sort[i] = finalHours[i];
}
for (int i = 7; i > -1; i--)
{
System.out.println(sort[i]);
}
}
}
result is
Employee 7 worked 41hours
Employee 6 worked 37hours
Employee 5 worked 28hours
Employee 4 worked 32hours
Employee 3 worked 31hours
Employee 2 worked 20hours
Employee 1 worked 28hours
Employee 0 worked 34hours
use int[] alternative String[] so you can obatin that result.
bye.

Large arraylist, finding the product of certain amount of elements

I have an array list which contains the numbers below, and what i am trying to do is find the product of every 16 numbers.
try {
for (int z = 0; z < 1000; z++) {
System.out.println(list.subList(z, z + 16));
the above prints this
[7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2]
[3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4]
[1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9]
[6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1]
etc......
my solution was to put every line above in to an array and find the product of that array.However i am stuck, was wondieering if anyone can provide me a few pointers on about how to take a hit at this
list.subList(z, z + 16);
for(int i = 0; i < list.subList(z, z+16).size();i++){
Ar[i] = list.get(z);
}
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
If you had a ArrayList of ArrayList then you could do
ArrayList <ArrayList> listofList = new ArrayList ();
for (int z = 0; z < 1000; z = z + 16) {
int endpoint = z + 16; // check to see not bigger than 1000
ArrayList thisList = list.subList(z, endpoint);
listOfList.add (thisList);
System.out.println(thisList);
}
But there again you may want to just add up as you go.
ArrayList thisList = list.subList(z, endpoint);
int prod = 1;
for (int x : thisList) {
prod *= x;
}
If you look at the printout that you are showing, you will that it is just moving one number each time - not what you want.
This is the solution I came up with. Instead of printing out the answer, you coudl add it to an array or something and use it later. Fyi I subbed the zeroes for 1's so I knew it was working.
public static void main(String[] args) throws Exception
{
int[] arr = {7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 1, 6, 2, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 1, 6, 2, 4, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 1, 6, 2, 4, 9};
int counter = 0;
int product = 1;
for (int i : arr)
{
if (counter < 16)
{
product *= i;
counter++;
}
if (counter >= 16)
{
System.out.println(product);
product = 1;
counter = 0;
}
}
}
This is what printed (again I subbed the zeroes for ones):
60011280
34292160
102876480

Rearranging an array in Java

If I have an array:
int[] myArray = new int[10]
for (int i = 0; i < myArray.length; i++) {
myArray[i] = i;
}
//resulting array: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
How can I move everything behing the 4 up one space, and send the 4 to the back? Example:
this:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
into this:
{0, 1, 2, 3, 5, 6, 7, 8, 9, 4}
How about this:
int[] myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.arraycopy(myArray, 5, myArray, 4, 5);
myArray[myArray.length-1] = 4;
In the above code, I'm using the arraycopy method to copy a range of 5 numbers starting from index 5, to index 4 in the array, and then simply set a 4 in the last position.
Notice that using arraycopy is much faster than copying the values in a loop, since it's usually implemented as a native operation which copies memory positions.
EDIT :
A more generic solution, a method for sending to the back a given position in the array:
public static void sendBack(int[] array, int idx) {
int value = array[idx];
System.arraycopy(array, idx+1, array, idx, array.length-idx-1);
array[array.length-1] = value;
}
For your example, call it like this:
sendBack(myArray, 4);
// now myArray is {0, 1, 2, 3, 5, 6, 7, 8, 9, 4}
Like this?
int start = 4;
int temp = myArray[start];
for(int i = start; i < myArray.length - 1; i++) {
myArray[i] = myArray[i+1];
}
myArray[myArray.length-1] = temp;
It's the fastest way I can guess...
Use System.arraycopy.

Categories