Predicament with arrays and expansion - java

So I know that the java convention is to use ArrayList<> when it comes to expansions and many other applications. The typical array cannot expand. My java course is elementary so we are still reviewing over arrays right now. As much as I want to use an arraylist I cant. How do I make it to where I store only elements that satisfy the condition in my counter array?
public int[] above100Degrees()
{
int[] blazing = new int[temps.length];
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
blazing[i] = temps[i];
}
}
return blazing;
}
Output
The temperature above 100 degrees is: 0 0 0 0 0 0 0 103 108 109

Just count how many elements match your filter first, then create the array, then populate it. It means you'll need to go through the array twice, but there are no really nice alternatives unless you want to end up creating multiple arrays. So something like:
public int[] above100Degrees() {
// First work out how many items match your filter
int count = 0;
// Are you allowed to use the enhanced for loop? It's not necessary, but it
// makes things simpler.
for (int temp : temps) {
if (temp > 100) {
count++;
}
}
// Create an array of the right size...
int[] ret = new int[count];
// ... and populate it.
int index = 0;
for (int temp : temps) {
if (temp > 100) {
ret[index++] = temp;
}
}
return ret;
}

I would use a loop to find how many are above 100 before assigning the array.
public int[] above100Degrees()
{
int newArrayLength=0;
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
newArrayLength++;
}
}
int[] blazing = new int[newArrayLength];
int positionInNewArray = 0;
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
blazing[positionInNewArray] = temps[i];
positionInNewArray++;
}
}
return blazing;
}

You could do a ternary operation
resultString = "The temperature above 100 degrees is: ";
for(int i = 0; i < blazing.length; i++){
resultString += blazing[i] != 0 ? blazing[i] : "";
}
Note: This would require more memory than JonSkeets answer, but could potentially be more efficient. If your expect your array length to get very large, go with JonSkeet's answer. In other words, this won't scale well.

One way is to count things before setting up the array. Another way is to set up the array first and keep track of the count, then create a new array:
public int[] above100Degrees()
{
int[] blazing = new int[temps.length];
int count = 0;
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
blazing[count++] = temps[i];
}
}
// At this point, `count` is the number of elements you're going to return;
// and the first `count` elements of `blazing` hold those elements, while the
// remaining elements of `blazing` are garbage
int[] result = new int[count];
for ( int i = 0; i < count; i++ )
result[i] = blazing[i];
return result;
}
This approach would be better if the condition you're testing for takes a lot of time to calculate (as opposed to temps[i] > 100, which hardly takes any time). You could use Arrays.copy to create the result array, but if you can't use ArrayList you probably can't use Arrays.copy either.

Your code is keeping the cells in blazing array for i <= 100; You need to ignore these and start populating from i = 101.

Related

Changing 2D ArrayList code to 2D array code

I found this code online and it works well to permute through the given array and return all possible combinations of the numbers given. Does anyone know how to change this code to incorporate a 2D array instead?
public static ArrayList<ArrayList<Integer>> permute(int[] numbers) {
ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
permutations.add(new ArrayList<Integer>());
for ( int i = 0; i < numbers.length; i++ ) {
ArrayList<ArrayList<Integer>> current = new ArrayList<ArrayList<Integer>>();
for ( ArrayList<Integer> p : permutations ) {
for ( int j = 0, n = p.size() + 1; j < n; j++ ) {
ArrayList<Integer> temp = new ArrayList<Integer>(p);
temp.add(j, numbers[i]);
current.add(temp);
}
}
permutations = new ArrayList<ArrayList<Integer>>(current);
}
return permutations;
}
This is what I have attempted:
public static int[][] permute(int[] numbers){
int[][] permutations = new int[24][4];
permutations[0] = new int[4];
for ( int i = 0; i < numbers.length; i++ ) {
int[][] current = new int[24][4];
for ( int[] permutation : permutations ) {
for ( int j = 0; j < permutation.length; j++ ) {
permutation[j] = numbers[i];
int[] temp = new int[4];
current[i] = temp;
}
}
permutations = current;
}
return permutations;
}
However this returns all zeroes. I chose 24 and 4 because that is the size of the 2D array that I need.
Thanks
It’s not really that easy. The original code exploits the more dynamic behaviour of ArrayList, so a bit of hand coding will be necessary. There are many correct thoughts in your code. I tried to write an explanation of the issues I saw, but it became too long, so I decided to modify your code instead.
The original temp.add(j, numbers[i]); is the hardest part to do with arrays since it invloves pushing the elements to the right of position j one position to the right. In my version I create a temp array just once in the middle loop and shuffle one element at a time in the innermost loop.
public static int[][] permute(int[] numbers) {
// Follow the original here and create an array of just 1 array of length 0
int[][] permutations = new int[1][0];
for (int i = 0; i < numbers.length; i++) {
// insert numbers[i] into each possible position in each array already in permutations.
// create array with enough room: when before we had permutations.length arrays, we will now need:
int[][] current = new int[(permutations[0].length + 1) * permutations.length][];
int count = 0; // number of new permutations in current
for (int[] permutation : permutations) {
// insert numbers[i] into each of the permutation.length + 1 possible positions of permutation.
// to avoid too much shuffling, create a temp array
// and use it for all new permutations made from permutation.
int[] temp = Arrays.copyOf(permutation, permutation.length + 1);
for (int j = permutation.length; j > 0; j--) {
temp[j] = numbers[i];
// remember to make a copy of the temp array
current[count] = temp.clone();
count++;
// move element to make room for numbers[i] at next position to the left
temp[j] = temp[j - 1];
}
temp[0] = numbers[i];
current[count] = temp.clone();
count++;
}
assert count == current.length : "" + count + " != " + current.length;
permutations = current;
}
return permutations;
}
My trick with the temp array means I don’t get the permutations in the same order as in the origianl code. If this is a requirement, you may copy permutation into temp starting at index 1 and shuffle the opposite way in the loop. System.arraycopy() may do the initial copying.
The problem here is that you really need to implement properly the array version of the ArrayList.add(int,value) command. Which is to say you do an System.arraycopy() and push all the values after j, down one and then insert the value at j. You currently set the value. But, that overwrites the value of permutation[j], which should actually have been moved to permutations[j+1] already.
So where you do:
permutation[j] = numbers[i];
It should be:
System.arraycopy(permutation,j, permutations, j+1, permutations.length -j);
permutation[j] = numbers[i];
As the ArrayList.add(int,value) does that. You basically wrongly implemented it as .set().
Though personally I would scrap the code and go with something to dynamically make those values on the fly. A few more values and you're talking something prohibitive with regard to memory. It isn't hard to find the nth index of a permutation. Even without allocating any memory at all. (though you need a copy of the array if you're going to fiddle with such things without incurring oddities).
public static int[] permute(int[] values, long index) {
int[] returnvalues = Arrays.copyOf(values,values.length);
if (permutation(returnvalues, index)) return returnvalues;
else return null;
}
public static boolean permutation(int[] values, long index) {
return permutation(values, values.length, index);
}
private static boolean permutation(int[] values, int n, long index) {
if ((index == 0) || (n == 0)) return (index == 0);
int v = n-(int)(index % n);
int temp = values[n];
values[n] = values[v];
values[v] = temp;
return permutation(values,n-1,index/n);
}

Adding 26 objects to an array list, but only 13 are being added

I'm creating a "Deal or no deal" code assignment for school. I am trying to creating 26 "case" objects in my "cases" arraylist using a for loop, however when I try to test my code it will not let me access anything above index 13. I get an arraylist out of bounds error.
public void createCases()
{
int amount;
int counter1 = 1;
int amountFound;
int allzero;
//Make a list of possible winning amounts
int amounts[] = new int[26];
for(int i = 0; i <= 25; i++) {
amounts[i] = counter1;
counter1++;
}
//Copy the winning amounts from amounts to amountsRandom... randomly.
int amountsRandom[] = new int[26];
for(int i = 0; i <= 25; i++) {
do {
amountFound = (int)(Math.random() * 25);
} while(amountFound == 0);
amountsRandom[i] = amounts[amountFound];
amountFound = 0;
}
//Take the amounts in index order and make them part of the case objects in the array list.
for(int i = 0; i <= 25; i++) {
cases.add(new Case(i++ , amountsRandom[i-1]));
}
}
I declared my cases arraylist outside of this method so it can be accessed by the entire class.
You are incrementing i twice: once in the for loop, once in the constructor call.
In your for loop where you add the Case ojects to the cases arraylist
for(int i = 0; i <= 25; i++) {
cases.add(new Case(i++, amountsRandom[i-1]));
}
You are calling i++ two times, so i will iterate twice per loop. Because of this, it only loops through 13 times instead of 26. You might want to change it to new Case(i + 1, amountsRandom[i-1])

Storing in an array

I'm trying to get a string to read a file, that then stores all the digits in an array that can be recalled one by one in another loop. Name the array digitStorage please :D Here's my current bit of code:
for (int i = 0; i <= 40000 ; i++) {
String digit;
if ( i <=39998)
digit = pictureFile.substring(i, i+1);
else
digit = pictureFile.substring(39998,39999);
My question :
What to do, how could I do this, how would I get it to read each digit (single integers) 1 by 1 and then store them 1 by 1 in an array that could be later recalled, each number corresponds to a color that would be used to sketch a picture in a graphics window (there are 40,000 single digit integers in a file that i've already worked out how to read) ?
Cheers.
As you have mentioned that you have already read the file and you want to store it in some kind of array. Below code will work.
List<String> list = new ArrayList<String>();
for (int i = 0; i <= 40000 ; i++) {
String digit;
if ( i <=39998)
list.add(pictureFile.substring(i, i+1));
else
list.add(pictureFile.substring(39998,39999));
}
If you want List of Integer then us.
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i <= 40000 ; i++) {
String digit;
if ( i <=39998)
list.add(Integer.parseInt(pictureFile.substring(i, i+1)));
else
list.add(Integer.parseInt(pictureFile.substring(39998,39999)));
}
You can iterate through list after this.
Your question is not very clear, but I believe this should do it:
int [] digitStorage = new int[40000];
for (int i = 0; i <= 40000 ; i++) {
if ( i <=39998)
int[i] = Integer.parseInt(pictureFile.substring(i, i+1));
else
int[i] = Integer.parseInt(pictureFile.substring(39998,39999));
Based on your comments and question, the easiest solution I can think of is to use String.toCharArray() and Character.digit(char, int) like
char[] chars = pictureFile.toCharArray();
int[] digitStorage = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
digitStorage[i] = Character.digit(chars[i], 10);
}
System.out.println(Arrays.toString(digitStorage));

set even to 0 and odd to 1

I'm playing around with double arrays and am trying to set all the even elements of an array to 0 and all of the odd elements of the array to 1. Everything looks okay to me, but when I run it I get a bunch of errors. Not sure what's wrong; I've been looking at it for a while with no luck. Any advice on how to fix the errors it gives would be great, thanks!
Code:
public class SetOf0and1 {
public static void main(String[]args)
{
int [][] numbers1 = {{4,2,5}, {2,4,1}, {1,3}};
System.out.println("Before setting elements between 0 and 1: ");
displayArray(numbers1);
setEvenRowsTo0OddRowsTo1 (numbers1);
System.out.println("After setting the elements between 0 and 1");
displayArray(numbers1);
}
public static void setEvenRowsTo0OddRowsTo1(int [][]array)
{
for(int i=0; i<array.length;i++)
{
for(int j=0; j<array[i].length;j++)
{
if(i%2 == 0)
array[i][j]=0;
else
array[i][j]=1;
}
}
}
public static void displayArray(int [][]array)
{
for(int i=0;i<array.length;i++)
{
for( int j=0; j<array.length;j++)
{
System.out.println(array[i][j] + " " );
}
System.out.println();
}
}
}
Errors given:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at SetOf0and1.displayArray(SetOf0and1.java:38)
at SetOf0and1.main(SetOf0and1.java:10)
public static void displayArray(int [][]array)
{
for(int i=0;i<array.length;i++)
{
for( int j=0; j<array.length;j++)
^^^^^^^^^
{
System.out.println(array[i][j] + " " );
}
System.out.println();
}
Your inner loop should stop at array[i].length.
In the method displayArray, the line:
for( int j=0; j<array.length;j++)
Should be:
for( int j=0; j<array[i].length;j++)
array.length does not return the length you thing it does! You have a 2 dimentional array. So if we say you have array[x][y] then array.length will be x and array[i].length (for 0 <= i < x) will be y. This could be different depending on the length of the array on that index. (so the formula does not exactly apply like that)
int [][] numbers1 = {{4,2,5}, {2,4,1}, {1,3}};
this statement initializes an array with three arrays of the legthes 3, 3 and 2!!!
(in the third block you have only two elements !!! - {1,3})
In your displayArray-method you use two times ...
array.length
... to distinct the size of the loop
that sets the number of loops to 3 ... But last block is only two elements long -> errror.
Use this instead for the second loop:
for( int j=0; j<array[i].length;j++)
If you want to check if a number is odd, you can do it this way:
int answer = thenumber % 2;
'thenumber' is the integer to check if it is even.
Then 'answer' would be 0 if the number was even.
And if you want to loop through the array and do it:
for (int i = 0; i < numbers1.length(); i++)
{
if (numbers1[i] % 2 == 0) {
//EVEN
numbers1[i] = 0;
}
else if (numbers1[i] % 2 == 1) {
//ODD
numbers1[i] = 1;
}
}
And, even more compact:
for (int i = 0; i < numbers1.length(); i++)
{
numbers1[i] %= 2;
}
Edit: I forgot that it was an array you had! I was thinking about ArrayList! Fixed.

Adding elements to last array position

Im trying to add an element to an array at its last position in Java, but I am not able to...
Or rather, I don't know how to. This is the code at the moment:
String[] values = split(line, ",");
int[][] coordinates = new int[2][values/2];
for(int i = 0; i < values.length; i++) {
if(i % 2 == 0) { //THIS IS EVEN VALUES AND 0
coordinates[0][coordinates[0].length] = values[i];
} else { //THIS IS ODD VALUE
coordinates[1][coordinates[1].length] = values[i];
}
}
EDITED VERSION:
String[] values = split(line, ",");
int[][] coordinates = new int[2][values/2];
int x_pos = 0;
int y_post = 0;
for(int i = 0; i < values.length; i++) {
if(i % 2 == 0) { //THIS IS EVEN VALUES AND 0
coordinates[0][x_pos] = values[i];
x_pos++;
} else { //THIS IS ODD VALUE
coordinates[1][y_pos] = values[i];
y_pos++;
}
}
values is being read from a CSV file. My code is I believe wrong, since it will try to add the values always at the maximum array size for coordinates[] in both cases.
How would I go around adding them at the last set position?
Thanks!
/e: Would the EDITED VERSION be correct?
Your original code has two problems:
it addresses the array badly, the las element in a Java array is at position length-1, and this would result in an ArrayOutOfBoundsException
even if you'd correct it by subtracting 1, you would always overwrite the last element only, as the length of a Java array is not related to how many elements it contains, but how many elements it was initialised to contain.
Instead of:
coordinates[0][coordinates[0].length] = values[i];
You could use:
coordinates[0][(int)Math.round(i/2.0)] = values[i];
(and of course, same with coordinates[1]...)
EDIT
This is ugly of course:
(int)Math.round(i/2.0)
but the solution I'd use is far less easy to understand:
i>>1
This is a right shift operator, exactly the kind of thing needed here, and is quicker than every other approach...
Conclusion: this is to be used in a live scenario:
Use
coordinates[0][i>>1] = values[i];
EDIT2
One learns new things every day...
This is just as good, maybe a bit slower.
coordinates[0][i/2] = values[i];
If you know you'll definitely have an even number of values you can do
for(int i = 0; i < values.length / 2; i++) {
coordinates[0][i] = values[2*i];
coordinates[1][i] = values[2*i + 1];
}
You have to store the last position somewhere. .length gives you the size of the array.
The position in the array will always be the half of i (since you put half of the elements in one array and the other half in the other).
String[] values = split(line, ",");
int[][] coordinates = new int[2][values/2];
for(int i = 0; i < values.length; i++) {
if(i % 2 == 0) { //THIS IS EVEN VALUES AND 0
coordinates[0][ i / 2] = values[i];
} else { //THIS IS ODD VALUE
coordinates[1][ i / 2 + 1 ] = values[i];
}
}
The array index for java is from "0" to "array length - 1".
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
why not:
String[] values = split(line, ",");
int[][] coordinates = new int[2][values/2];
for(int i = 0; i < values.length; i+=2) {
coordinates[0][i/2] = values[i];
coordinates[1][i/2] = values[i+1];
}

Categories

Resources