I am creating a seating chart in java that displays the cost of each seat in a two dimensional array:
public class MovieTheater {
public static void main(String[] args) {
final int rows = 10;
final int columns = 10;
int i;
int j;
int[][] seating = {
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 }
};
However, when I attempt to print the array:
for (i = 0; i < rows; i++) {
System.out.print(rows[i]);
for (j = 0; j < columns; j++) {
System.out.print(columns[j]);
}
}
}
}
I receive an error that states: array required, but int found
Is this a problem with my array format or a syntax problem for my print solution?
You do columns[j], but columns is an int, so you can't access it like an array. Same with rows[i]. What you should do is in the inner loop
System.out.println(seating[i][j]);
"columns" and "rows" have been defined as int, not as arrays of type int. the index value of rows and columns can be used to access rows and columns of array(seating). and it can be printed with a single print statement:
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++)
System.out.print(seating[i][j]);
You are not actually accessing your array object in your for loop.
Try this:
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
System.out.print(seating[i][j]);
}
}
You code doesn't work because you're trying to use an integer as an array.
In fact rows and columns are two integers (value 10); your array is seating.
When the compiler compiles your code it sees something like this:
for (i = 0; i < 10; i++) {
System.out.print(10[i]);
for (j = 0; j < 10; j++) {
System.out.print(10[j]);
}
}
which is impossible.
What you really want is:
for (i = 0; i < rows; i++) {
for(j = 0; j < columns; j++) {
System.out.print( seating[i][j] );
}
}
Related
I'd like some assistance on comparing two arrays of different dimensions. I want to check if the single dimensional array is a sub array of the 2 dimensional array. Here's what I tried:
public static void compare() {
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
for (int i = 0; i < x.length - (x.length) + 1; i++) {
System.out.println(Arrays.toString(x[2]));
for (int j = 0; j < y.length - (y.length) + 1; j++) {
System.out.println(Arrays.toString(y));
if (x[2].equals(y)) {
System.out.println("match");
} else {
System.out.println("no match");
}
}
}
}
I don't know what exactly you want, but as i can see - you need something like:
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
for (int[] aX : x) {
System.out.println(Arrays.toString(x[2]));
if (Arrays.equals(aX, y)) {
System.out.println("match");
} else {
System.out.println("no match");
}
}
}
}
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
for (int[] i : x) {
if (Arrays.equals(y, i)) {
System.out.println("match");
}
}
import java.util.*;
public class ArrayMatch {
public static void main(String[] args){
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
String yArray = Arrays.toString(y);
boolean match = false;
for(int i = 0; i < x.length; i++) {
String comparison = Arrays.toString(x[i]);
if(comparison.equals(yArray)) {
match = true;
}
}
if(match) {
System.out.println("Match");
}
else
System.out.println("No match");
}
}
Here is your output:
Match
int[][] x = { { 23, 33, 46, 50, 56 }, { 3, 8, 65, 34, 90 }, { 2, 7, 46, 50, 56 } };
int[] y = { 2, 7, 46, 50, 56 };
int[] z = x[2].clone();
int count = 0;
for (int i = 0; i < z.length; i++) {
if (z[i] != y[i]) {
System.out.println("Not Match");
break;
} else {
count++;
if (count == y.length) {
System.out.println("Match");
break;
} else {
continue;
}
}
}
Output is:
Match
I have copied 2D Array's 2nd element to another array(z[]) because array can be compared to one by one element not directly one array to another array.
The following is my implementation of Selection Sort:
package algorithm.selectionsort;
public class SelectionSort {
public static void main(String[] args) {
int[] myArray = selectionSort(new int[] { 9, 9, 9, 8, 7, 73, 32, 109, 1100, 432, 321, 0 });
for (int element : myArray) {
System.out.print("" + element + " ");
}
}
public static int[] selectionSort(int[] a) {
int min;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
return a;
}
}
I noticed that my instructor codes it slightly differently:
public static int[] selectionSort(int[] a) {
int min;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
return a;
}
Both implementations work. I'm curious as to what the difference here is. Is it efficiency?
The difference between your instructor's and yours is that he iterate through the array and for each element, search for the minimum, then perform a swap with the element after the wall index.
For yours, you iterate through the array and for each element, while searching for the minimum, if current value is < then the current tentative min, perform a swap with the element after the wall index.
So instead of swapping n times, you could possible swap n*n times for worst case:
Your swap for just one pass (worst case):
100, 90, 88, 70, 55, 43, 32, 28, 19, 10
90, 100, 88, 70, 55, 43, 32, 28, 19, 10
88, 100, 90, 70, 55, 43, 32, 28, 19, 10
70, 100, 90, 88, 55, 43, 32, 28, 19, 10
55, 100, 90, 88, 70, 43, 32, 28, 19, 10
43, 100, 90, 88, 70, 55, 32, 28, 19, 10
32, 100, 90, 88, 70, 55, 43, 28, 19, 10
28, 100, 90, 88, 70, 55, 43, 32, 19, 10
19, 100, 90, 88, 70, 55, 43, 32, 28, 10
10, 100, 90, 88, 70, 55, 43, 32, 28, 19
Your instructor's swap for just one pass (worst case):
100, 90, 88, 70, 55, 43, 32, 28, 19, 10
10, 90, 88, 70, 55, 43, 32, 28, 19, 100
In essence, you swap the values while in the midst of searching for the min. The "min" you swapped may not be the lowest value in the array.
ofcouse your instructor's code is more efficiency and more elegant.
What is Selection Sort?
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
If the length of the list to be sorted is n, then just n times of exchange should be done, but in your code, it's n*(n-1)*(n-2)....
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
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;
}
}
I am trying to generate a random tour from the graph. I am using the adjacency-list method.
There is a problem with the vertices. When I add a vertex to a particular list, the vertex gets added to all the lists in the graph. I do not understand why! Here's the code:
public static void main(String[] args) {
defaultData(6);
}
public static void defaultData(int n) {
Integer costs[] = { 26, 95, 38, 74, 80, 73, 73, 92, 22, 97, 13, 81, 41,
17, 4, 2, 47, 54, 21, 68, 78, 4, 77, 3, 66, 55, 99, 42, 62, 39, 8, 36,
53, 74, 26, 8, 42, 66, 30, 58, 69, 14, 49, 39, 85, 98, 72, 3, 18, 99,
96, 66, 64, 36, 17, 44, 70, 0, 8, 14, 62, 41, 84, 59, 94, 27, 5, 27,
96, 10, 15, 52, 43, 20, 2, 86, 45, 43, 32, 17, 49, 92, 9, 15, 6, 49,
72, 7, 51, 21, 2, 26, 63, 82, 98, 48, 21, 96, 16 };
ArrayList<ArrayList<Integer>> costGraph = new ArrayList<>();
ArrayList<ArrayList<Integer>> completeGraph = new ArrayList<>();
Random rand = new Random(System.currentTimeMillis());
int costIndex = 0;
for (int i = 0; i <= n; i++) {
ArrayList<Integer> cost = new ArrayList<>();
ArrayList<Integer> edge = new ArrayList<>();
for (int j = 0; j <= n; j++) {
if (i == j) {
continue;
}
edge.add(j);
cost.add(costs[costIndex]);
costIndex++;
}
completeGraph.add(edge);
costGraph.add(cost);
}
System.out.println(completeGraph);
ArrayList<ArrayList<Integer>> dummyGraph =
(ArrayList<ArrayList<Integer>>)completeGraph.clone();
ArrayList<ArrayList<Integer>> randomTour = new ArrayList<>();
ArrayList<Integer> dummyList = new ArrayList<>();
for (int i = 0; i <= n; i++) {
randomTour.add(dummyList);
}
System.out.println(dummyGraph);
int edgeCount = 0;
Integer row = rand.nextInt(n);
Integer start = row;
while(edgeCount <= n-1){
//dummyList = dummyGraph.get(row);
// To keep the bounds of the random equal
// to the new reduced size of the lists in the graph
Integer col = dummyGraph.get(row).get(rand.nextInt(n-edgeCount));
randomTour.get(row).add(col);
System.out.println(row);
System.out.println(randomTour);
edgeCount++;
for(int k = 0; k < n; k++)
dummyGraph.get(k).remove(row);
row = col;
}
randomTour.get(row).add(start);
System.out.println(randomTour);
}
I would be very grateful for a timely response. Thanks in advance!
You don't want to do this:
for (int i = 0; i <= n; i++) {
randomTour.add(dummyList);
}
It keeps adding the same reference lots of times, so all the ArrayLists in the ArrayList are actually the same object.
Instead you want to do this:
for (int i = 0; i <= n; i++) {
randomTour.add(new ArrayList<Integer>());
}
That way the ArrayList instances in the ArrayList are all different.
I hope this answer was timely enough!