Java array initialization - java

Given a bunch of temperatures, I am supposed to put them into an array that sorts them by every 10 degrees. I am pretty confused. I was able to print out all the temperatures from a provided txt file, but now I am not sure how to put them into an array. There should be 11 indexes( <=9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, 70-79, 80-89, 90-99, and >=100). I am just really confused on how to use arrays.

I think you should use multidimensional array or a hash table that sort temperatures in a range from 1 to 10 and from 10 to 20

Put to the array the upper edges:
int[] a = {10, 20, 30, ..., Integer.MAX_INTEGER}
It defines intervals 0-9, 10-19, etc.

Related

Suitable data structure for Java 1000*1000 matrix.

I am practising java programming. I encountered a problem which requires 1000 x 1000 matrix which stores integers value less than 1500
I would want to navigate across all the elements
I might need to fetch max element and its 4 adjacent elements.
What is the best data structure which doesn't affect performance?
1 2 3 4
5 6 7 8
9 10 11 12
12 14 15 16
for the element 11 --> 7, 10, 12, 15 are adjacent elements.
what is wrong with the 2d array data structure?
to get an adjecent of a number at i,j return [i-1][j],[i+1][j],[i][j+1],i[j-1] (you will have to deal with cases where i is zero etc)...
as performance goes, its O(1), doesn't get any better than that...
If you are talking about finding the location of the element. if the matrix is sorted you can simply do a binary search.
A 2dim Array of shorts:
-> short [][] matrix = new short[1000][1000];
| Added variable matrix of type short[][] with initial value [[S#1794d431
Generate in a second, few MB in size. What could be better than that?

Which data type should I choose for a sequence of sorted integers?

I have a a row of int numbers, e.g. 1 2 3 7 8 9. They are sorted.
I often need to insert numbers in this row, so that the row stays sorted, e.g. 4 => 1 2 3 4 7 8 9. Sometimes I have to read the row from the start to a number, that depends on the numbers in the row.
Which data type to choose best and how to insert the new number best?
If your sequence does not have repetitions you can use a SortedSet<Integer>, say, a TreeSet<Integer>, so that every time you and an element the sequence will remain sorted.
If the sequence does have repetitions check out Guava's sorted multiset.
try ArrayList. it is much easier to manipulate than a simple array. if you need to only work with primitives, this can still be done with an array of ints.
ArrayList<Integer> foo = new ArrayList<integer>;
foo.add(2,4) //puts a 4 at index 2, shifting everything else down

Best data structure to store and manipulate my data?

I am writing a simple Java program that will input a text file which will have some numbers representing a (n x n) matrix where numbers are separated by spaces. for ex:
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
I then want to store these numbers in a data structure that I will then use to manipulate the data (which will include, comparing adjecent numbers and also deleting certain numbers based on specific rules.
If a number is deleted, all the other numbers above it fall down the amount of spaces.
For the example above, if say i delete 8 and 9, then the result would be:
() 2 3 ()
1 6 7 4
5 1 2 3
4 5 6 7
so the numbers fall down in their columns.
And lastly, the matrix given will always be square (so always n x n, where n will be always given and will always be positive), therefore, the data structure has to be flexible to virtually accept any n-value.
I was originally implementing it in a 2-d array, but I was wandering if someone had an idea of a better data structure that I could use in order to improve efficiency (something that will allow me to more quickly access all the adjacent numbers in the matrix (rows and columns).
Ultimately, mu program will automatically check adjacent numbers against the rules, I delete numbers, re-format the matrix, and keep going, and in the end i want to be able to create an AI that will remove as many numbers from the matrix as possible in the least amount of moves as possible, for any n x n matrix.
In my opinion, you yo know the length of your array when you start, you are better off using an array. A simple dataType will be easier to navigate (direct access). Then again, using LinkedLists, you will be able to remove a middle value without having to re-arrange the data inside you matrix. This will leave you "top" value as null. in your example :
null 2 3 null
1 6 7 4
5 1 2 3
4 5 6 7
Hope this helps.
You could use one dimensional array with the size n*n.
int []myMatrix = new myMatrix[n * n];
To access element with coordinates (i,j) use myMatrix[i + j * n]. To fall elements use System.arraycopy to move lines.
Use special value (e.g. Integer.MIN_VALUE) as a mark for the () hole.
I expect it would be fastest and most memory efficient solution.
Array access is pretty fast. Accessing adjacent elements is easy, as you just increment the relevant index(s) (being cognizant of boundaries). You could write methods to encapsulate those operations that are well tested. Having elements 'fall down' though might get complicated, but shouldn't be too bad if you modularize it out by writing well tested methods.
All that said, if you don't need the absolute best speed, there are other options.
You also might want to consider a modified circularly linked list. When implementing a sudoku solver, I used the structure outlined here. Looking at the image, you will see that this will allow you to modify your 2d array as you want, since all you need to do is move pointers around.
I'll post a screen shot of relevant picture describing the datastructure here, although I would appreciate it if someone will warn me if I am violating some sort of copy right or other rights of the author, in which case I'll take it down...
Try a Array of LinkedLists.
If you want the numbers to auto-fall, I suggest you to use list for the coloumns.

copying column of a 2D array as 1D array in java?

Let's say I have a 2D array:
int[][] a = new int[4][3];
populated such that:
1 2 3
4 5 6
7 8 9
2 5 7
Is there any shortcut method in java to extract lets say column 1 as single array:
array1 = {1 4 7 2};
Currently what I am doing is traversing through the whole 2D matrix and with if condition (if j==0), I traverse over the rows and add values to 1D array.
Just wondering if there is any standard method offered in java for such tasks.
No there is no shortcut to doing this. You have to loop over the arrays, switching the x & y indices.
There is no such build-in method. You have to write a simple loop.
You might want to consider use of a matrix library. But this is pretty simple stuff - if this is all you need, you can probably write it quicker than you can get up to speed on a library.

Extracting three minimums

I want to obtain the minimum of a double array and the other two minimum values. In total I wan to obtain the 3 smaller values of the array. I am not using the class array, but I am using a double[].
Easiest way is to call
Arrays.sort()
and take the first 3 values.
Otherwise, you can simply loop through the array and keep track of the three smallest, much like you would the smallest.
double[] dlist = {17.0, 10.0, 44, 7, 4.0, 33, 24, 10, 48.0, 49.0};
Arrays.sort (dlist);
System.out.println (dlist [0] + " " + dlist [1] /*...*/);
Similarly to above, you could loop through and store the smallest one, then remove from the array. Then do it again, and again. But I think the ways mentioned above are more efficient.
Well, if you can't use the Arrays class at all, you will probably want 3 variables, one to hold each of the values you are trying to get. Just start off by setting them equal to the first 3 elements in the array (if there are at least 3, otherwise just set a few of them).
Then use a for loop to go through the rest of the elements in the array. If an element is smaller than one or more of the numbers you already found, get rid of the largest of those numbers and add this one to the list of smallest numbers instead.
1. declare 3 variables
2. set variables equal to first 3 elements in array
3. loop from index 3 (4th element) to the length of the array
a. see which of the already found numbers is bigger than the current element (if any)
b.replace the biggest of the found numbers with the new number if at least one was found
4. print out or return the numbers you found

Categories

Resources