For an array, how can i take all the values at even indexes and add to nameArray and all the values at odd indexes and add to scoreArray? I got this code but it isn't working.
String[] inputArray = {"john", "10", "frank", "14"}
for (int j = 0; j == inputArray.length; j++) {
if ((j % 2) == 0) {
nameArr.add(inputArray[j]);
} else {
scoreArr.add(inputArray[j]);
}
}
You probably mean for (int j = 0; j < inputArray.length; j++)
j == inputArray.length is evaluated to false at the first iteration so your loop doesn't run.
However, you could get rid of the if statement (assuming that your inputArray always contains a name associated a score, i.e always contains pair values) :
for (int j = 0; j < inputArray.length; j+=2) {
nameArr.add(inputArray[j]);
scoreArr.add(inputArray[j+1]);
}
Or you could also use a Map<String, Integer> to associate each name with its corresponding score (assuming names are unique) :
for (int j = 0; j < inputArray.length; j+=2)
m.put(inputArray[j], Integer.parseInt(inputArray[j+1]));
You probably mean to have a < in your loop and not a == and also, try using inputArray and not split2[] like this:
for (int j = 0; j < inputArray.length; j++) {
if ((j % 2) == 0) {
nameArr.add(inputArray[j]);
} else {
scoreArr.add(inputArray[j]);
}
}
What you want to do is look through each element in the array, to do this you want to go through every element starting at 0 and ending at the lengthof the array -1 (because arrays are 0 indexed). Once you are in the loop you want to check if it is an even or odd number using the modulo operator.
for (int j = 0; j < inputArray.length; j++){
if ((j % 2) == 0) {
nameArr.add(inputArray[j];
} else {
scoreArr.add(inputArray[j];
}
}
Related
I have been working an a good size but single threaded java application. In it I have a function which loops through a matrix of about size[300][10]. I have done a lot of things above this code snippet and have 3 other matrices of similar sizes as local variables. I was having problems with the loop not going through the first value (table[0][0]) when I noticed that the code:
System.out.println("");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if(i == 0 && j == 0){System.out.println("looped through 0 0");}
// a bunch of other stuff
}
}
prints out:
looped through 0 0
but the code:
//System.out.println("");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if(i == 0 && j == 0){System.out.println("looped through 0 0");}
// a bunch of other stuff
}
}
does not print anything.
Why would this be? Have I run out of Java heap space? Have I overflowed? Is this a compiler error?
I found what was wrong. Above the loop I had another loop and had left a printf statement in there. I am still not sure why this occurs but I was able to reproduce it in the following class.
public class TestJavaPrintfError {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.printf(" ");
}
String[][] table = new String[300][6];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if (i == 0 && j == 0) {
System.out.println("looped through 0 0");
}
}
}
}
}
I am on developing on eclipse neon. Anyone know why this happens?
Is that a joke?
Just scroll the console to the rightmost, you will see the print.
You are using System.out.printf(" "); 100 times, it output alot of spaces and push your pritnt to the right.
I have a class with a matrix initialized with all 0 and 1 in a specific position:
public class MatrixTest {
/*# spec_public #*/ int[][] griglia;
//#requires true;
//#ensures griglia[2][3] == 1;
public MatrixTest() {
griglia = new int[6][6];
for (int i=0; i < 6; i++) {
for (int j=0; j < 6; j++){
griglia[i][j] = 0;
}
}
griglia[2][3] = 1;
}
}
I would like to add an invariant to check that i always have only one 1 cell with a value of 1 and 35 cells with a value of 0. I tried doing that:
//# public invariant (\num_of int i, j; i >= 0 && i < 6 && j >= 0 && j < 6; griglia[i][j] == 1) == 1;
But it gives me invariant error just after the constructor. How can i iterate through a matrix to check a property?
After a lot of research, it seems like you can only do it with \forall, other commands didn't work for me.
\forall int i; i >= 0 && i < matrix.length; (\forall int j; j >= 0 && j < matrix[i].length; /* your check here */)
So, just don't use openJML if you are working with multi-dimensional arrays
I am trying to create a program that will take an array of integers, say {2,0,32,0,0,8} that can be of any length, and make it so all of the nonzero numbers are to the left at the lower indexes, and all the zeros are moved to the end.
For example, {2,0,32,0,0,8} becomes {2,32,8,0,0,0}.
This array can be of any length and contain any nonnegative integers.
This is what I have so far:
public static int[] moveLeft(final int[] a) {
for (int i = 0; i < a.length; i++) {
if (a[i] != 0) {
for (int j = 0; j < a.length; j++) {
if (a[j] == 0) {
a[j] = a[i];
a[i] = 0;
}
}
}
}
return a;
}
However, when I do this, it doesn't work for the first and second characters. If I have {1,2,0,1} it will return {2,1,1,0} when it should return {1,2,1,0}. Any help?
Your inner loop should stop before index i. Change this
for (int j = 0; j < a.length; j++) {
to
for (int j = 0; j < i; j++) {
And then your code works for me.
Given an array of integers with rows of different lengths, is it possible to print the whole two-dimensional array but doing so column by column? I understand how to do it row by row but I am struggling with this.
int[][] a = new int[5][];
a[0] = new int[4];
a[1] = new int[2];
a[2] = new int[5];
a[3] = new int[3];
a[4] = new int[1];
int longestRowLength = a[0].length;
for(i = 1; i < a.length; i++)
{
if(a[i].length > longestRowLength)
longestRowLength = a[i].length;
}
for(i = 0; i < a.length; i++)
{
for(j = 0; j < a[i].length; j++)
{
a[i][j] = rand.nextInt(10);
System.out.print(a[i][j]);
}
System.out.println();
}
for(j = 0; j < longestRowLength; j++)
{
for(i = 0; i < a.length; i++)
{
if(a[i].length < longestRowLength)
continue;
System.out.print(a[i][j]);
}
}
}
I have done this but the issue is with how to recognize we are going out of bounds with one of the arrays. My if(a[i].length < longestRowLength doesn't work as it will not even print any numbers if its length is not the longest ones. How can I achieve this?
EDIT:
Ok I have changed that line to:
if(longestRowLength - a[i].length > 0 && (j+1) > a[i].length)
continue;
System.out.print(a[i][j]);
Now it works but it prints the columns as rows. Is there anyway to make it print column by column but to make it print just like it would with rows? (P.S. yeah the first condition of the if statement is unecessary).
Replace your last loop with:
for(j = 0; j < longestRowLength; j++)
{
for(i = 0; i < a.length; i++)
{
if(a[i].length <= j)
continue;
System.out.print(a[i][j]);
}
System.out.println();
}
Prints the columns one by one.
Instead of:
if(a[i].length <= j)
continue;
you can do:
if(a[i].length <= j) {
System.out.print(' ');
continue;
}
to leave a space for arrays which are too short. This way you print the transposed “jagged” matrix.
I've been told the below code is = O(MN) however, I come up with O(N^2). Which is the correct answer and why?
My thought process:
nested for loops plus if statements --> (O(N^2)+O(1)) + (O(N^2)+O(1)) = O(N^2)
Thank you
public static void zeroOut(int[][] matrix) {
int[] row = new int[matrix.length];
int[] column = new int[matrix[0].length];
// Store the row and column index with value 0
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[0].length;j++) {
if (matrix[i][j] == 0)
{
row[i] = 1;
column[j] = 1;
}
}
}
// Set arr[i][j] to 0 if either row i or column j has a 0
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[0].length; j++)
{
if ((row[i] == 1 || column[j] == 1)){
matrix[i][j] = 0;
}
}
}
}
What does M and N refers to? My assumption is that it refers to "rows" and "columns" respectively. If it is so, then the equation is O(MN) because you loop through M number of N times.
O(N^2) will be correct IF rows and columns are equal.