why does the for-loop go through the args three times? - java

I want to know why the for-loop counts through the args three times and doesn't stop after the first loop? this is my code :
public class test2 {
public static void main(String[] args) {
int N = args.length;
int[] x = new int[N];
int count = 0;
for (int i = 0; i < x.length; i++) {
x[i] = Integer.parseInt(args[i]);
}
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (x[j] != x[j-1]) {
count++;
}
System.out.println(count);
}
}
System.out.println(N-count);
}
}

I think you mean to do this:
for (int j = 1; j < N; j++) {
if (x[j] != x[j-1]) {
count++;
}
System.out.println(count);
}
System.out.println(N-count);
instead of:
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (x[j] != x[j-1]) {
count++;
}
System.out.println(count);
}
}
System.out.println(N-count);

Related

Two dimensional array to one dimensional array in Java

I have a two dimensional array and I fill it with scanner. I want to copy the elements that start with letter 'a' to a new one dimensional array without using ArrayList. Please advise on what I can do to get this code functioning properly. the question is how can I know the new array size while I don't know how many words start with letter a
Here is what I have so far:
import java.util.Scanner;
class Untitled {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int c2 = -1;
String[] name2 = new String[count];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
c2++;
temp = arr[i][j];
name2[c2] = temp;
count++;
temp = "";
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
}
}
A two dimensional arrray of size [n][n] is equal to one dimensional array of size n. If you want to copy them on proper place then you can use this formula, it is useful if you later want to copy these elements back to twodimensional array at proper places:
int v = i * n + j; // i and j your loops and n is length of rows or colums.
array[v] = array[i][j];
for in your codes it's like:
int v = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i * arra.length +j;
name2[v] = arr[i][j];
count++;
Ok here is a working code:
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
System.out.println("Enter the name: ");
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int v = 0;
String[] name2 = new String[arr.length*arr[0].length];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i *+arr[0].length + j;
name2[v] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
System.out.println("printing without nulls");
//if you don't want null to be printed then do this:
for (int i = 0; i < name2.length; i++) {
if(name2[i] != null)
System.out.println(name2[i]);
}
}
I did it with two nested for loop one for indicating the array size and the other for filling the elements into the array, it does the work but is there any way to do this better
public static void student(String[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
size++;
}
}//inner
}//outer
String[] name2 = new String[size];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
name2[count] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}

Java out patterns

I'm currently trying to get this output using nested loops:
For the life of me I cannot figure out how to get the # signs to increment by +2 each time. Any help would be greatly appreciated!
public class PrintPatterns {
public static void main(String[] args) {
pattern1();
}
private static void pattern1() {
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < 10; j += 2); {
System.out.print("# ");
}
for (int j = 0; j < 2; j++) {
System.out.print(". ");
}
for (int j = 1; j < 10 - i; j++) {
System.out.print("x ");
}
System.out.println();
}
}
}
This code does what you want:
private static void pattern1() {
StringBuilder stringBuilder = new StringBuilder();
for (int ats = 2; ats <= 10; ats += 2) {
for (int j = 0; j < ats; j++) {
stringBuilder.append("# ");
}
stringBuilder.append(". . ");
for (int j = 0; j <= 10 - ats; j++) {
stringBuilder.append("x ");
}
stringBuilder.append("\n");
}
System.out.println(stringBuilder.toString());
}
One mistake you did in your code is that you put a ; after your for loop, this will end the loop right there.
Also do not use System.out.println() in loops. As using IO will slow down your application. Use StringBuilder to build strings and then output all at once.
Instead of:
for (int j = 0; j < 10; j += 2); {
System.out.print("# ");
}
Try:
for (int j = 0; j < 2 * i; j += 1) {
System.out.print("# ");
}
public class PrintPatterns
{
public static void main(String[] args)
{
pattern1();
}
private static void pattern1()
{
for(int i = 1; i <= 10; i++)
{
for(int j = 1; j < i+2; j++)
{
System.out.print("# ");
}
for(int j = 0; j < 2; j++)
{
System.out.print(". ");
}
for(int k = 10-i; k > 0; k--)
{
System.out.print("x ");
}
System.out.println();
}
}
}

How do I reverse a boolean array in java?

I want to reverse a boolean array. free is the array. Here's my code:
public boolean[][] free = new boolean[6][6];
free = !free;
I get an error saying the operator ! is undefined. What should I do?
EDIT
It is not empty. I just want to inverse the values.
public boolean[][] free = new boolean[6][6];
void initFree(State s) {
for (int i = 0; i < nbcars; i++){
if (horiz[i]){
for (int j = 0; j < (len[i]-1); j++)
free[moveon[i]][s.pos[i]+j]=true;
}
if (!horiz[i]){
for (int j = 0; j < (len[i]-1); j++)
free[s.pos[i]+j][moveon[i]]=true;
}
}
free = !free;
}
You get operator ! is undefined simply because Java doesn't define ! operator on a two dimensional boolean array.
One way you could achieve what you want is by iterating over each value and then use the ! operator.
for(int i=0; i<free.length; i++)
{
for(int j=0; j<free[i].length; j++)
{
free[i][j] != free[i][j];
}
}
Here's a simple example you can modify for your uses. I included an invert method.
public class StackOverflowExample {
public static void main(String[] args) {
int ROW = 10, COL = 10;
boolean[][] ATwoDBoolArray = new boolean[ROW][COL];
PutValuesInArray(ATwoDBoolArray);
printArray(ATwoDBoolArray);
invertArray(ATwoDBoolArray);
printArray(ATwoDBoolArray);
}
private static boolean[][] PutValuesInArray(boolean array[][]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = false;
}
}
return array;
}
private static void printArray(boolean[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
}
private static boolean[][] invertArray(boolean array[][]){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j]){
array[i][j] = false;
}else{
array[i][j] = true;
}
}
}
return array;
}
}
This method inverts all the values in a given 2-dimensional boolean array.
static void invertBooleanArray(boolean[][] arr) {
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
arr[i][j] = !arr[i][j];
}

Matrix Transpose in Java

I have a method transpose in class Matice. When i apply this method on my m2.matice i can see during debugging that my matrix is being transposed, but when i print m2 i get the same matrix as before.
public class Main {
public static void main(String[] args) {
Matice m1 = new Matice(5);
m1.matrixFill(0, 5);
m1.matrixPrint();
Matice m2 = new Matice(3);
m2.matrixFill(-5, 10);
m2.matrixPrint();
m2.transpose();
m2.matrixPrint();
}
}
package matice;
/**
*
* #author Adam Rychter
*/
public class Matice {
int[][] matice;
private int n;
public Matice(int n) {
this.n = n;
if(n > 0) {
matice = new int[n][n];
}
}
public void matrixPrint(){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.format("%5d", matice[i][j]);
}
System.out.println("");
}
System.out.println("");
}
public void matrixFill(int a, int b){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matice[i][j] = (int) (Math.random() * (a + b + 1) - a);
}
}
}
public void transpose(){
int tmp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
tmp = matice[i][j];
matice[i][j] = matice[j][i];
matice[j][i] = tmp;
}
}
}
}
Your problem is that you're transposing each element twice ! once when i=row, j=col, and once when i=col, j=row, with the net effect of leaving the matrix as it was. The easiest way I see to fix it is do the swap only if i>j (and then test to make sure :) this is a suggestion, not working code :)
Note that j goes from i to n, not 1 to n; also note need for two tmp variables.
public void transpose(){
int tmp1, tmp2;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
tmp1 = matice[i][j];
tmp2 = matice[j][i];
matice[i][j] = tmp2;
matice[j][i] = tmp1;
}
}
Like this, maybe:
public void transpose(){
Matice mL = new Matice(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mL.matice[i][j] = matice[j][i];
}
}
matice = mL.matice;
}
See alternate, more efficient answer above.
public void transpose(){
int tmp;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
tmp = matice[i][j];
matice[i][j] = matice[j][i];
matice[j][i] = tmp;
}
}
}

Having Trouble using For Loops to make two triangles of different characters fit into a rectangle?

Examples of input:
3
4
Examples of output (assume that spaces = new lines.)
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
So far, the fragment of code that attempts to print this is (Assume that all variables are pre-defined):
public int getSize()
{
for (int i = size; i > 0; i--){
for (int j = 1; j < size; j++){
out.print("Q");
out.print("H");
}
out.println("");
}
return 0;
}
It just prints: (assume that spaces = new lines.)
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
For input of 5. I'm not quite sure how to make it print only the number of times of its respective integer value. Can someone explain?
You could break the inner loop it two, like this:
for (int i = size; i > 0; i--) {
for (int j = 0; j < i; j++) {
out.print("Q");
}
for (int j = i; j < size + 1; j++) {
out.print("H");
}
out.println();
}
Output:
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
Or if you don't want to break the loop, you can use the ternary operator:
for (int i = size; i > 0; i--) {
for (int j = 0; j < size + 1; j++) {
out.print(j < i ? 'Q' : 'H');
}
out.println();
}
Try this
for (int i = 0; i < size; i++) {
for (int j = 1; j <= size-i; j++) {
System.out.print("Q");
}
for (int k = 0; k <= i; k++) {
System.out.print("H");
}
System.out.println("");
}
try this code block instead:
int j=0;
for (int i = size; i > 0; i--)
{
j=0;
while(j < i)
{
out.print("Q");
j++;
}
j=i;
while(j < size+ 1)
{
out.print("H");
j++;
}
out.println();
}
Tested with sample inputs. Working fine
public int getSize() {
for (int i = 1; i < size+1; i++) {
for (int j = 0; j < size+1; j++) {
int Qtimes = size-i;
if(j <= Qtimes) {
System.out.print("Q");
} else{
System.out.print("H");
}
}
System.out.println("");
}
return 0;
}
This works if the input is 4 - for example -change it to any number
public int getSize()
{
int cnt = 0;
int i,j,k = 0;
for ( i = 4; i > 0; i--){
for ( j = 0; j < i; j++){
System.out.print("Q");
}
cnt ++;
for( k = 0 ; k <cnt ; k++) {
System.out.print("H");
}
System.out.println("");
}
return 0;
}
output is
QQQQH
QQQHH
QQHHH
QHHHH

Categories