Write a Csv file to a Matrix like structure [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a csv file with this format, where in the first column I have the number of repetitions, in the second the number of days and in the third the time of execution.
Here's the representation:
1 7 131
2 7 71
3 7 114
[.....]
9 7 338
10 7 394
11 7 437
12 7 496
1 31 171
[.....]
12 31 1894
1 91 437
[.....]
10 91 4394
[.....]
I want to process this file to transform it in this way, I can use Java or Excel Macros
7 31 91 183 366
1 131 171 437 866 1906
2 71 305 867 173 3460
3 114 493 136 261 5356
4 159 596 182 356 6916
5 210 800 249 468 8762
6 223 919 378 605 11296
7 270 107 354 644 12898
8 270 123 401 746 14265
9 338 145 398 903 15487
10 394 164 439 934 16971
11 437 174 507 104 18941
12 496 189 527 110 21378
Where the first column of the Csv file remain the first column of the file, but not repeated. Then the period [7,31,...,366] must be placed as the first row.
Inside this matrix there will be placed all elements?
How can i do with Java or directly with macro inside Excel?
Thanks

What about a Map <Integer, Map <Integer, Integer>> being the first key the column and the inner key the number of days? The inner value would be the time of execution.
After being filled, you could print it doing something like:
print map.get(1).getKeys()
for (Entry entry : map.getKeys()
print entry.key()
for (Integer innerValue : map.get(entry.key())
print innerValue
The maps should be ordered for this to work...

Related

How to use TSPlib to solve traveling salesman problem (TSP) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am actually working on TSP problem, and I need to test program, I am using the data from the TSPlib API(Symmetric traveling salesman problem) :
https://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/tsp/
So I so confused about the data file, for example, is a280.tsp represent the coordinates of cities or the distances because when I am using it like coordinate it gives me wrong values,
please, someone can help !!!
Each line is : node number + x coordinate + y coordinates
Node_id x y
The content of the a280.tsp file is the following (opened with a text editor):
NAME : a280
COMMENT : drilling problem (Ludwig)
TYPE : TSP
DIMENSION: 280
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 288 149
2 288 129
3 270 133
4 256 141
5 256 157
6 246 157
7 236 169
8 228 169
9 228 161
10 220 169
...
280 280 133
EOF
For further details, have a look here

I want to render data column wise in table in itext 7.0.1

Generally in table cells go row wise but I want to render it column wise.
Data is stored in an array list.
----------- means page break
Expectation is:
1 6
2 7
3 8
4 9
5 10
-------------
11 16
12 17
13 18
But generally it comes like this
1 2
3 4
5 6
7 8
9 10
-------------
11 12
13 14
15 16
17 18

List<String>.remove does not appear to be working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am developing in Java on Netbeans IDE 7.4 on Windows 7. I am using the following code to progressively remove and shift elements in a list of strings.
static void RemoveSubsequence(List<String> entries, int start, int end){
int removeIndex=start+1;
String debugStr;
for (int i=removeIndex; i<end; ++i){
debugStr=entries.remove(removeIndex);
}
}
The following are the results for 13 elements in the list starting with index removeIndex. The first column shows the initial values while the next two columns shows the results of the first two iterations. The first iteration works perfectly but the second iteration changes what should be 624 (second row) to 634.
634 624 624
624 624 634
624 624 634
624 634 624
634 624 634
624 634 624
634 624 634
624 634 776
634 776 672
776 672 624
672 624 776
624 776 776
776 776 672
I think i understand what you are intending to do now, sorry.
If you want to remove a subset of the list, what you should do is keep removing the item at index "Start", and remove that same index "End - Start" times.
int times = end - start;
For (int i= 0; i < times; i++)
{
String listItem = list.remove(start);
}
Now your list has removed all the required items.
I would remove the entry from the List first, and then assign the string value of debugStr. See if that helps.

How to find a cycle/repeats in a string?

I need to detect a cycle/sequence in a string and return the first occurrence. How should I go about doing it?
Example :
2 0 5 3 1 5 3 1 5 3 1
The first sequence to occur is 5 3 1.
There is no rules. The sequence can be half the string length, for example
5 3123 1231 231 31 231 41 452 3453 21 312312 5 3123 1231 231 31 231 41 452 3453 21 312312
The sequence is 5 3123 1231 231 31 231 41 452 3453 21 312312
Have you studied Floyds cycle-finding algorithm? That may well help you if you want to find cycles. Very easy to implement as well.
Clarification based on the comments: cycle means a sequence of digits which repeats immediately. So
1 1
would be a cycle
1 3 1
wouldn't because the potential cycle of 1s is interupted by 3
1 3 1 3
is a cycle (1 3).
So a basic algorithm could look like this.
Iterate of the String.
For each Digit find it next occurrence in the String. If nothing found continue with the next character.
If a next occurrence is found compare the sequence from the current digit up to the following occurrence with the sequence of same length beginning at the next occurence. If they are the same you found a cycle. If not continue with the next occurence.

Java sort array based on three column

On my quest learning Java I come across one doubt.
For sorting a unidimensional array we can use Arrays.sort() but if I want to sort a bidimensional array based on three columns? Is there any option to d that or do I have to code it for my self (something like three nested loops)?
Here is an example input:
13 2 28 36
1 4 56 17
4 2 5 40
2 4 41 55
9 5 48 12
19 2 25 12
20 5 13 8
15 3 51 30
12 5 39 59
17 3 56 40
3 1 56 46
7 3 28 51
8 5 14 58
5 3 34 15
14 4 53 2
18 4 38 57
6 2 16 25
16 3 17 13
10 5 41 33
11 1 13 57
Columns are int and this is stored in an array of ints.
I wanna sort by column 2, if equal numbers are found, then sort by column 3 and finally, if equals found, sort by column 3.
The output should be this:
11 1 13 57
3 1 56 46
4 2 5 40
6 2 16 25
19 2 25 12
13 2 28 36
16 3 17 13
7 3 28 51
5 3 34 15
15 3 51 30
17 3 56 40
18 4 38 57
2 4 41 55
14 4 53 2
1 4 56 17
20 5 13 8
8 5 14 58
12 5 39 59
10 5 41 33
9 5 48 12
Is there a simple way for doing this?
Remember that I'm new at Java.
regards,
Favolas
Simple use Comparator and use Arrays.sort(arr, comparator);
It depends on how you get the data.
In general, you could give your own Comparator to Collections.sort.
Use the comparator below
class MyArrayComparator implements Comparator<Integer[]> {
#Override
public int compare(Integer[] o1, Integer[] o2) {
if (o1[1] == o2[1]) {
if (o1[2] == o2[2]) {
return o1[3].compareTo(o2[3]);
}
else {
return o1[2].compareTo(o2[2]);
}
}
return o1[1].compareTo(o2[1]);
}
}
Use the following sort method
Collections.sort(yourListOfArry, new MyArrayComparator());
Implementing an Comparator on arrays of arrays seems kind of dangerous to me because you have to ensure manually, that all arrays in the second dimension are of the same length and the semantics of the components of the array must be the same at all times - for these kind of constraints objects should be used.
If the second dimension in the array has some meaning (i.e. it is always four becouse three are coordinates and the fourth is the value), you should consider not to model it as an array of arrays but an array of objects with four member variables (e.g. xCoord, yCoord, zCoord and value). You can then make this class implement the Comparable interface and implement the compareTo method. Then the Arrays.sort() method will sort according to the given compareTo method.

Categories