Search Results

Search found 7 results on 1 pages for 'hansdampf'.

Page 1/1 | 1 

  • Reversing permutation of an array in Java efficiently

    - by HansDampf
    Okay, here is my problem: Im implementing an algorithm in Java and part of it will be following: The Question is to how to do what I will explain now in an efficient way. given: array a of length n integer array perm, which is a permutation of [1..n] now I want to shuffle the array a, using the order determined by array perm, i.e. a=[a,b,c,d], perm=[2,3,4,1] ------ shuffledA[b,c,d,a], I figured out I can do that by iterating over the array with: shuffledA[i]=a[perm[i-1]], (-1 because the permutation indexes in perm start with 1 not 0) Now I want to do some operations on shuffledA... And now I want to do the reverse the shuffle operation. This is where I am not sure how to do it. Note that a can hold an item more than once, i.e. a=[a,a,a,a] If that was not the case, I could iterate perm, and find the corresponding indexes to the values. Now I thought that using a Hashmap instead of the the perm array will help. But I am not sure if this is the best way to do.

    Read the article

  • How to set specific column settings in JTable?

    - by HansDampf
    How can I make my JTable show specific stuff for each column? Can I use an Object for more than one column? I want to have a column showing the date (dd.mm.yyyy) and another to show the time (hh:mm) but as I have it now it only shows the String representation of the Date object as a whole. public class AppointmentTableModel extends AbstractTableModel { ... @Override public Object getValueAt(int rowIndex, int columnIndex) { return appointments.get(rowIndex).getByColumn(columnIndex); } } public class Appointment { ... public Object getByColumn(int columnIndex) { switch (columnIndex) { case 0: return date; case 1: return date; case 2: return sample; case 3: return sample; case 4: return history; case 5: return comment; } return null; } }

    Read the article

  • Java Swing: How to add a CellRenderer for displaying a Date?

    - by HansDampf
    I have a Table: public class AppointmentTableModel extends AbstractTableModel { private int columns; private int rows; ArrayList<Appointment> appointments;... So each row of the table contains one Appointment. public class Appointment { private Date date; private Sample sample; private String comment; private ArrayList<Action> history; public Appointment(Date date, Sample sample, String comment) { this.date = date; this.sample = sample; this.comment = comment; this.history = new ArrayList<Action>(); } public Object getByColumn(int columnIndex) { switch (columnIndex) { case 0: return date;//Date: dd:mm:yyyy case 1: return date;//Time mm:hh case 2: return sample;//sample.getID() int (sampleID) case 3: return sample;//sample.getNumber string (telephone number) case 4: return sample;//sample.getName string (name of the person) case 5: return history;//newst element in history as a string case 6: return comment;//comment as string } return null; I added in comments what this one is going to mean. How would I create CellRenderers to display it like this. table.getColumnModel().getColumn(1).setCellRenderer(new DateRenderer()); I also want to add the whole row to be painted in red when the date is later then the current date. And then another column that holds a JButton to open up another screen with the corresponding Appointment as parameter.

    Read the article

  • Java: How do I implement a method that takes 2 arrays and returns 2 arrays?

    - by HansDampf
    Okay, here is what I want to do: I want to implement a crossover method for arrays. It is supposed to take 2 arrays of same size and return two new arrays that are a kind of mix of the two input arrays. as in [a,a,a,a] [b,b,b,b] ------ [a,a,b,b] [b,b,a,a]. Now I wonder what would be the suggested way to do that in Java, since I cannot return more than one value. My ideas are: - returning a Collection(or array) containing both new arrays. I dont really like that one because it think would result in a harder to understand code. - avoiding the need to return two results by calling the method for each case but only getting one of the results each time. I dont like that one either, because there would be no natural order about which solution should be returned. This would need to be specified, though resulting in harder to understand code. Plus, this will work only for this basic case, but I will want to shuffle the array before the crossover and reverse that afterwards. I cannot do the shuffling isolated from the crossover since I wont want to actually do the operation, instead I want to use the information about the permutation while doing the crossover, which will be a more efficient way I think.

    Read the article

  • How to use CellRenderer for GregorianCalendar?

    - by HansDampf
    So I have been trying to use the example from Tutorial and change it so it fits my program. The getColumnValue method returns the object that holds the information that is supposed to be displayed. Is this the way to go or should it rather return the actual String to be displayed. I guess not because that way I would mix the presentation with the data, which I was trying to avoid. public class IssueTableFormat implements TableFormat<Appointment> { public int getColumnCount() { return 6; } public String getColumnName(int column) { if(column == 0) return "Datum"; else if(column == 1) return "Uhrzeit"; else if(column == 2) return "Nummer"; else if(column == 3) return "Name"; else if(column == 4) return "letzte Aktion"; else if(column == 5) return "Kommentar"; throw new IllegalStateException(); } public Object getColumnValue(Appointment issue, int column) { if(column == 0) return issue.getDate(); else if(column == 1) return issue.getDate(); else if(column == 2) return issue.getSample(); else if(column == 3) return issue.getSample(); else if(column == 4) return issue.getHistory(); else if(column == 5) return issue.getComment(); throw new IllegalStateException(); } } The column 0 and 1 contain a GregorianCalendar object, but I want column 0 to show the date and 1 to show the time. So I know using cellRenderers can help here. This is what I tried. public class DateRenderer extends DefaultTableCellRenderer { public DateRenderer() { super(); } public void setValue(Object value) { GregorianCalendar g =(GregorianCalendar) value; value=g.get(GregorianCalendar.HOUR); } } But the cell doesnt show anything, what is wrong here?

    Read the article

1