I have a problem with filling a LinkedHashMap with GregorianCalendar-Objects.
I creat a GregorianCalendar starttime. Then I fill it into an ArrayList and add 200 Milliseconds 50 times. After that, I fill these values in a Map together with a double from another ArrayList. When I make a System out of the Map it only gives me the last value of the time list but all values from the double list.
starttime = new GregorianCalendar(2013, 0, 1, 13, 0, 0);
starttime.set(Calendar.MILLISECOND, 0);
GregorianCalendar time = new GregorianCalendar();
time.setTimeInMillis(starttime.getTimeInMillis());
for (int i = 0; i < 50; i++) {
time.add(Calendar.MILLISECOND, 200);
timeList.add(time.getTimeInMillis());
}
for (int i = 0; i < 50; i++) {
time.setTimeInMillis(timeList.get(i));
inputMap.put(time, valueList.get(i));
}
for (Entry<GregorianCalendar, Double> entry : inputMap.entrySet()) {
System.out.println(entry.getKey().getTime().toString()+" "
+entry.getKey().get(Calendar.MILLISECOND)+ " = " + entry.getValue());
}
I found a solution. The Problem is, that time is a reference in the inputMap. So you have to create a new GregorianCalendar in the filling loop.
for (int i = 0; i < 50; i++) {
GregorianCalendar gregCal = new GregorianCalendar();
gregCal.setTimeInMillis(starttime.getTimeInMillis());
inputMap.put(gregCal, valueList.get(i));
starttime.add(Calendar.MILLISECOND, 200);
}
Related
I have a problem when I'm trying to a loop in a DataTable that a dataset contains.
I'm doing a loop like this:
for(int i = 0; i<ds.Tables[0].Rows.Count - 1 ; i++)
The problem is that I can't get the value of the last line with this one, but if I try to get rid of the "-1" and do a loop on the whole table, I'll have an out of range exception.
This out of range exception is because I have to check if the value of a line "i" is equal to the value of a line "i+1", like this:
if (ds.Tables[0].Rows[i]["Release_No"] != ds.Tables[0].Rows[i + 1]["Release_No"])
So if I do it in a loop, when the index is on the last line it will check if the last line is equal to i+1, and it's out of the table.
So I was trying to check if the index is on the last line, then just get the value of the last line, but it seems like it doesn't work.
if(ds.Tables[0].Rows.IndexOf(ds.Tables[0].Rows[i]) == ds.Tables[0].Rows.Count)
If anyone has an idea, let me know, and of course if it is not clear enough let me know, I'll give more information and more code.
Thanks for your help and your time!
Check if it's the last record, first.
I like to refactor code to read as close to sentence form as possible, explaining what you want it to do using named variables and methods, and that often gets me unlocked.
Try to make each line of code do one thing, and one thing only, like check if it is the last row:
var data = ds.Tables[0].Rows;
var lastRow = data.Count - 1;
for(int i = 0; i < lastRow ; i++)
{
if (i == lastRow){
// This is the last row. Handle the last row here.
}
else
{
// Handle all other rows here
var currentRecord = data[i];
var nextRecord = data[i + 1];
if (currentRecord["Release_No"] != nextRecord["Release_No"])
{
// Handle unique Releases...
}
}
}
Use less than or equal to like this
for(int i = 0; i<=ds.Tables[0].Rows.Count - 1 ; i++)
I hope this may get what you want.
Something like this is better ?
var lastRow = data.Count - 1;
var data = ds.Tables[0].Rows;
for(int i = 0; i< lastRow; i++)
{
testFirstCum = Convert.ToInt32(ds.Tables[0].Rows[i]["EDI_Accum_Quantity"]);
if ( i == lastRow)
{
if (DBNull.Value.Equals(data[i]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
else
{
var col = ds.Tables[0].Columns;
var currentRecord = data[i];
var nextRecord = data[i + 1];
if(currentRecord["Release_No"] != nextRecord["Release_No"])
{
for (int j = col[2].Ordinal; j < col.Count; j++)
{
if (DBNull.Value.Equals(data[i][j]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i][j]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
}
}
}
I have a QTableWidget with some columns and rows and want to add a filter for a particular column.
For that, I've added a QLineEdit in my Window.
I'm already able to filter the rows, when I add only one number in the QLineEdit:
for(int i=0; i<tableWidget->rowCount(); i++)
{
if(!tableWidget->item(i, column)->text().contains(lineEdit->text()))
{
tableWidget->hideRow(i);
}
}
(The slot is connected to the textEdited-Signal of the LineEdit)
What I want to do now:
When I write something like this in the QLineEdit: 10-30; Hide all rows, which doesnt have the number between 10 and 30 (>=10; <=30).
Somebody has an idea, how I can solve this?
This is my decision.
Check if lineEdit text contains two numbers.
QString test = ui->lineEdit->text();
QStringList lst = test.split('-');
if (lst.size() == 2)
Transform they in to integers.
int low = QString(lst[0]).toInt(), high = QString(lst[1]).toInt();
Now let's go to tableWidget and drop in less than or equal to these two numbers.
for (int i = 1; i <= 100; i++) {
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(i)));
}
connect(ui->lineEdit, &QLineEdit::textChanged, this, [=](const QString &test) {
QStringList lst = test.split('-');
if (lst.size() == 2) {
int low = QString(lst[0]).toInt(), high = QString(lst[1]).toInt();
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
int temp = ui->tableWidget->item(i, 0)->text().toInt();
if (temp < low || temp > high) {
ui->tableWidget->hideRow(i);
} else {
ui->tableWidget->showRow(i);
}
}
}
});
say I have a 1D array like
int[] array1d = {1,2,3}
I would like to convert it into 2D array2d[3][2] which holding 2 int that are different. E.g.:
1 2
1 3
2 3
currently I made this
int[] array1d = new int[3];
array1d[0] = 1;
array1d[1] = 2;
array1d[2] = 3;
int[][] array2d = new int[3][2];
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
array2d[i][j] = array1d[j];
}
}
but it gives me only 1,2.
Generally speaking, what you want is called combinations (in your example, of size 2 taken from a 3-sized array). So, order does not matter (e.g. [1, 2] equals [2, 1]).
As already specified in the comments, you should consider a more general solution and one can be found here. Besides the actual code, you will also find a code reviews from Codereview community.
i have done this using random numbers.try this code
` import java.util.Random;
public final class RandomInteger {
public static void main(String... aArgs){
Random randomGenerator = new Random();
int[] array1d = new int[3];
array1d[0] = 1;
array1d[1] = 2;
array1d[2] = 3;
int[] array2d = new int[3][2];
int randomInt;
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
randomInt = randomGenerator.nextInt(3);
array2d[i][j] = array1d[randomInt];
}
}
}
}
`
I am trying to find the minimum number in my array of integers, however, it returns 0.
import mpi.*;
import java.util.Random;
class AddIntSR
{
public static void main(String[] params) throws Exception
{
MPI.Init(params);
int me = MPI.COMM_WORLD.Rank();
int size = MPI.COMM_WORLD.Size();
final int CHUNKSIZE = 1;
final int ROOT = 0;
Random rg = new Random();
int [] bigBuf = new int[CHUNKSIZE *size];
int [] smallBuf = new int[CHUNKSIZE];
int [] minBuf = new int[1];
int localTotal = 0;
if (me == ROOT)
{
for(int i = 0; i< bigBuf.length; i++)
bigBuf[i] = rg.nextInt(10);
for(int i = 0; i< bigBuf.length; i++)
System.out.println("bigBuf "+bigBuf[i]);
}
MPI.COMM_WORLD.Scatter(bigBuf,0,CHUNKSIZE,MPI.INT,smallBuf,0,CHUNKSIZE,MPI.INT,ROOT);
if(me!= ROOT)
{
System.out.println("smallBuf "+me+ ": "+smallBuf[0]);
for(int i = 0; i < smallBuf.length; i++)
localTotal += smallBuf[i];
}
MPI.COMM_WORLD.Reduce(new int[]{localTotal},0,bigBuf,0,1,MPI.INT,MPI.MAX,ROOT);
MPI.COMM_WORLD.Reduce(new int[]{localTotal},0,minBuf,0,1,MPI.INT,MPI.MIN,ROOT);
if(me == ROOT)
{
System.out.println(bigBuf[0]);
System.out.println(minBuf[0]);
}
}
}
I am not sure why it does not work. The maximum function seems to work fine.
Also, how would I be able to access the integer that is sent to processor 0 so it is included in the min/max comparison?
Thank you.
The MIN reduction always results in 0 since localTotal is always 0 in rank ROOT and this is indeed the minimum value.
After the MPI.COMM_WORLD.Scatter call, all process including ROOT will have a piece of data in their smallBuf. Therefore you should remove the following conditional, i.e.:
if(me!= ROOT)
{
System.out.println("smallBuf "+me+ ": "+smallBuf[0]);
for(int i = 0; i < smallBuf.length; i++)
localTotal += smallBuf[i];
}
should become simply:
System.out.println("smallBuf "+me+ ": "+smallBuf[0]);
for(int i = 0; i < smallBuf.length; i++)
localTotal += smallBuf[i];
This is my Code.
Dt = BlObj.BlDynamic_Table("[USP_DynamicGridView]", 2);
DtOperation = BlObj.BlDynamic_Table("[USP_DynamicGridView]", 1);
for (int i = 0; i < DtOperation.Rows.Count; i++)
{
Dt.Columns.Add(DtOperation.Rows[i][0].ToString());
}
dgrDynamic.DataSource = Dt;
dgrDynamic.DataBind();
dgrDynamic.Columns.Count is resulting 0 even though there are columns surly.