So say I have an array with columns {item, quantity}. I am getting these values from an sql table, then sorting them by item. I am using a temporary variable "quantity" to manipulate the double value:
while(rs.next()){
item = rs.getString(1);
for(int i = 0 ; i < array.size ; i++){
if(item.equals(array[i][0]){
double quantity = rs.getDouble(2);
quantity = quantity + Double.parseDouble(array[i][1]);
array[i][1] = quantity;
}
}
//add code for adding items if not found in array
}
My question is will using this temporary variable be slower/create garbage while it iterates the array? Does Java properly dispose of this variable at the end of the if statement? I am asking because I am having issues with heap space, and I do not want to have to rewrite everything to accomodate this.
My other option is:
if(item.equals(array[i][0]){
array[i][1] = String.valueOf(Double.parseDouble(array[i][1]) + rs.getDouble(2));
}
Thanks for reading
It will always override quantity with new value, when old value will be keep of course in array. Quantity won't have impact on your memory especially that this is primitive.
If you will deal with complex type and you will do something like that, you will override with every loop the reference to object, so in array you will have all the time reference to same object.
Related
I'm using the CPLEX Java interface to do some MIP problems, the optimizer is doing well but I can't get values of vars by their names.
The definitions of my vars are in a for loop, so in the outer code, I can't use cplex.getValue() function to get their values.
Please remind me of any methods that can get all the values or get values by their names.
for (int i = 0; i < count; ++i){
// c1 is changing when i increase
IloNumVar[] x = new IloNumVar[c1];
for (int j = 0; j < c1; ++j) {
x[j] = cplex.numVar(0, 1, IloNumVarType.Int, "x" + String.valueOf(i) + "_" + String.valueOf(j));
}
}
...
cplex.solve();
How to retrieve all x values outer for loop after cplex.solve()?
Just don't try to recover them by their names. Keep your CPLEX variables in lists, arrays, dictionaries, structures, class instances or whatever in your code. Your IloNumVars are just normal objects that can be stored like any other. In your example code, just keep those IloNumVar arrays in a structure declared outside the loop (e.g. a list of those IloNumVar[] arrays).
If you really need to retrieve them by name, then put them in a dictionary, keyed on the variable name - again that would have to be declared outside your loop above. But that would be less efficient than just keeping them in your usual Java data structures as it would require extra lookup processing inside the ictionary to find the item by name.
I have a table with fields like empNo, Name , etc.
and i want java to generate numbers automatically.
for eg. if the number of the last employee was e009,
java should give me the number for the next one as e010.
I tried this and it works well but seems tedious.
rs.last();
String no,temp;
no = rs.getString(1);
int len = no.length();
len=len-1;
int n = Integer.parseInt(no.substring(1,no.length()));
n=n+1;
int length = (int)(Math.log10(n)+1);
no="E";
for(int i=1;i<=len-length;i++)
no=no+"0";
no=no+n;
txtNo.setText(no);
Have a static counter variable inside the class that represents your table's row. In its constructor, assign the empNo value to the value of the counter.
You could also add formatting to make the number a fixed size of, say, 3 in another method or within the constructor itself.
And don't forget to increment the counter within the constructor.
I have created a list of 2D arrays containing randomly generated number values for different locations.
public static int Prices[][] = new int[Cities.length][ItemNames.length];
public static List<int[][]> CityPrices = new ArrayList<int[][]>();
public static void NewDay()
{
for(int i = 0; i<Cities.length; ++i)
{
Prices[i] = PriceGenerator.ReturnPricesForCity(i);
//This method returns an array of random integers
}
CityPrices.add(Prices);
}
But then later when I want to retrieve the price history for a specific item for the amount of days passed, it returns the same value for each day
int Prices[] = new int[GlobalVariables.CityPrices.size()];
String sTest = "";
for(int i = 0; i < Prices.length; ++i)
{
Prices[i] = GlobalVariables.CityPrices.get(i)[spinCity.getSelectedItemPosition()][spinItem.getSelectedItemPosition()];
sTest = sTest + Prices[i] + ",";
}
In this case, the values returned by sTest was : 6055,6055,6055,6055,6055, for five consecutive days.
If I would for instance add a day, the values would change to a range of a new number, which in this case was : 7294,7294,7294,7294,7294,7294,
Please show me what I am doing wrong, as I have been trying to figure this one out the past 4 days with no luck.
Every element in your CityPrices list is the same: in each case, you are adding the Prices two-dimensional array. Your loop modifies Prices[i], but it doesn't change Prices, which is still a reference to the same two-dimensional array right the way through.
I think you're imagining it will pass the contents of the array in its current state, but it doesn't: it passes a reference to the array to the .add() method, so any subsequent changes to the array will be reflected in the contents of CityPrices.
If at the end of your loop you try
CityPrices.get(0) == CityPrices.get(1)
you'll see it returns true.
In the assignment: Prices[i] = GlobalVariables.CityPrices.get(i)[spinCity.getSelectedItemPosition()][spinItem.getSelectedItemPosition()]; you are basically referencing an int[][] at the same index for both dimensions.
On top of that, the spinCity.getSelectedItemPosition() invocation might be returning the same index at every iteration of your loop, hence your identical values.
It's hard to assume anything further as you haven't posted the code for spinCity.
I am trying to do check if an array is full and print out to the user he is unable to enter more books.
static Object[][] books=new Object[2][];
I am asking 3 values from the user and am storing to another array called "row".
Object[]row=new Object[3];
After that i loop through the books array and check if it has a null value inside of it and add the "row" array with the given values of the user.
the problem am having is i cant give any feedback if books array is full after entering two rows of values.
boolean empty=false;
while(empty==false){
for (int i = 0; i < books.length; i++) {
if(books[i]==null){
books[i]=row;
empty=true;
break;
}
}
}
Why not having a variable
int bookCounter = 0;
which you can increase/decrease everytime you add/delete books and then just check it against your max number of books (which I assume is books.length)
In this way you don't need to loop over the array, which is not efficient.
There is no need for the while loop. After the for loop ends, check your empty variable that you're already setting. If it's false, then give your feedback to the user.
Arrays are always full. Even each element is null. If you initialize it with the size of 10, then JVM will alocate memory and fill the all the arrays positions with default value. What is: 0 for primitives numbers and char type, false for primitive boolean type and null for Objects.
So, your code won't work with a int[] for instance. Because there won't exist any null element.
That's why you dont have a count() method... You can create a method that is named countNotNull(). But you'll need to interate for all the array long.
The best solution is to use a variable to count when you add/remove itens form array. As
Guillermo Merino said.
I'm trying to get a program so that it loops and adds up the sum of an array. My code appears to be working, with the exception that it states that the text[j] in adding = adding + text[j] is an incompatible type (I'm assuming data type). Earlier in the code, I have int adding = 0;. This is the erroneous code:
for (int j=0;j<=total;j++){
adding = adding + text[j];
System.out.println(text[j]);
}
where total is the limiting factor. If I put:
for (int j=0;j<= total;j++){
adding = adding + j;
System.out.println(text[j]);
}
the program compiles but gives 45, which is incorrect.
Why is this happening? Thanks!
The answer actually turned out to be outside the code given. I had set my array to be a String, not an int as it should have been.
If your text[] is String[] or char[] as the name suggests then I believe you are trying to update text[] elements with suffix j or adding, which you can write as:
If it is char[] then write
text[j] = (char)(adding + (int)text[j]);
If it is String[] then write
text[j]= text[j]+adding;
as required. It all depends on what is the data type of text[] and what are you trying to achieve?
Also as suggested in one of the answers, if total is length of the array, then change the comparison to < to avoind ArrayIndexOutOfBoundsException
Your second example, adds j into adding but prints text[j] value, which is nothing to do with the addition of adding and j.