Ok I'm going to post all of the code cos I know I'm really close to solving
this problem now.
This code is ListDemo2.java, and this is basically the main progrm thats
sets up all the applet stuff and displays the lists. What it can do is insert
an item onto the end of the list, make an ordered list from zero up to an
input in the text field(integer), make a random list, clear the list, delete
duplicates ( this is what i'm having trouble with ) and exit the applet.
This code is called ListByVector.java and is used to generate the lists andCode:import java.awt.*; import java.awt.event.*; /* new in 1.1 for EventListener */ import java.applet.Applet; import java.util.*; public class ListDemo2 extends Applet implements ActionListener, AdjustmentListener { private ListByVector list = new ListByVector(0); // setup a limit for displaying the list final int numRow = 5; final int numCol = 10; final int maxDisplay = numRow * numCol; private int scrollbarPos = 0; private TextField userInput; private Label messageLabel; private Label scrollbarLabel; private Label[] itemLabels; private Scrollbar listScroll; private Button insertButton; private Button mk_olistButton; private Button mk_rlistButton; private Button clearButton; private Button deleteButton; private Button exitButton; // ... adding more buttons here public void init() { // 1. define stuff on the first panel - control panel Panel controlPanel1 = new Panel(); userInput = new TextField("", 5); insertButton = new Button("insert"); mk_olistButton = new Button("mk_olist"); mk_rlistButton = new Button("mk_rlist"); clearButton = new Button("clear"); deleteButton = new Button("delete"); exitButton = new Button("quit"); controlPanel1.setLayout(new FlowLayout(FlowLayout.LEFT)); controlPanel1.add(userInput); controlPanel1.add(insertButton); controlPanel1.add(mk_olistButton); controlPanel1.add(mk_rlistButton); controlPanel1.add(clearButton); controlPanel1.add(deleteButton); controlPanel1.add(exitButton); // ... adding new buttons // for event handling we need these lines insertButton.addActionListener(this); mk_olistButton.addActionListener(this); mk_rlistButton.addActionListener(this); clearButton.addActionListener(this); deleteButton.addActionListener(this); exitButton.addActionListener(this); // ... any newly added buttons need the above line // 2. define stuff on the second panel - message panel Panel controlPanel2 = new Panel(); messageLabel = new Label("Initially, the list is empty"); scrollbarLabel = new Label(" "); controlPanel2.setLayout(new GridLayout(2,1)); controlPanel2.add(messageLabel); controlPanel2.add(scrollbarLabel); // 3. define stuff on the third panel - list display panel Panel listPanel = new Panel(); Panel listDisplay = new Panel(); itemLabels = new Label[maxDisplay]; listDisplay.setLayout(new GridLayout(numRow,numCol)); int tmp=0; for(tmp=0; tmp < maxDisplay; tmp++) { itemLabels[tmp] = new Label(" "); listDisplay.add(itemLabels[tmp]); } listScroll = new Scrollbar(Scrollbar.VERTICAL,0,0,0, list.sizeLimit()/numCol + 1); listScroll.addAdjustmentListener(this); /* new in 1.1 */ listPanel.setLayout(new BorderLayout()); listPanel.add( "West", listDisplay); listPanel.add( "East", listScroll); // finally, add the above 3 to the applet setLayout(new BorderLayout()); add("North", controlPanel1); add("Center", controlPanel2); add("South", listPanel); } // end of ListDemo's constructor public void actionPerformed(ActionEvent event) { String input_buffer; int k; // read a string from the text field input_buffer = new String(userInput.getText()); if(event.getSource() == clearButton) { list.clearUp(); showList(list); showMessage("list cleared"); } else if(event.getSource() == insertButton) { k = list.insertAtEnd(input_buffer); if(k>=0) { // inserted OK showList(list); showMessage( "inserted " + input_buffer + " at " + k); } else { // reached the limit, nothing inserted showMessage("sorry, you cannot insert any more"); } } else if(event.getSource() == mk_olistButton) { k = list.createOrderedList(input_buffer); if(k>0) { showList(list); showMessage("an ordered list is created, the list size is "+k); } else showMessage("please input the size of your list"); } else if(event.getSource() == mk_rlistButton) { k = list.createRandomList(input_buffer); if(k>0) { showList(list); showMessage("a random list is created, the list size is "+k); } else showMessage("please input the size of your list"); } /*Quick note about the code below, if I keep it as is with the showList(list) commented out it will not visibly delete the duplicates but the message that comes up saying how many duplicates were deleted gives the correct answer for the first time only, if I were to create another random list with a different set of duplicates then the message will be incorrect. Also if the showList(list) were to be uncommented then the program suffers from overflow problems??????*/ else if(event.getSource() == deleteButton) { k = list.deleteMultiple(); // showList(list); showMessage("deleted " +k + " duplicate items"); } else if(event.getSource() == exitButton) { System.exit(0); } } public void adjustmentValueChanged(AdjustmentEvent event) { if(event.getSource() == listScroll) { scrollbarPos = listScroll.getValue(); showScrollbarMessage( "the scrollbar position is " + scrollbarPos); showList(list); } } void showList(ListByVector list) { int i = scrollbarPos; int n = list.size(); int c = 0; i = i * numCol; while((i < n) && (c < maxDisplay)) { itemLabels[c].setVisible(true); itemLabels[c].setText(list.item(i)); i++; c++; } while(c < maxDisplay) { itemLabels[c].setVisible(true); itemLabels[c].setText(""); c++; } // clear input text field userInput.setText(""); } void showMessage(String Message) { messageLabel.setVisible(true); /* this line was not needed in java1.0 */ messageLabel.setText(Message); } void showScrollbarMessage(String Message) { scrollbarLabel.setVisible(true); /* this line was not needed in java1.0 */ scrollbarLabel.setText(Message); } public static void main(String args[]) { Frame f = new Frame("List Demo"); ListDemo2 appletListDemo = new ListDemo2(); f.setFont(new Font("Helvetica", Font.BOLD, 12)); f.setSize(700, 300); f.setBackground(Color.green); f.add(appletListDemo); appletListDemo.init(); f.setVisible(true); } }
then return the info back to ListDemo2.
Code:import java.util.*; public class ListByVector { private final int default_max_size = 4096; private int current_size; private int m; private Vector data; ListByVector(int initial_size) { data = new Vector(); current_size = initial_size; } public int sizeLimit(){ return default_max_size; } public int size(){ return current_size; } public String item(int which){ return (String)data.elementAt(which); } public void clearUp() { current_size = 0; } public int insertAtEnd(String input_item) { if(current_size < default_max_size) { data.insertElementAt(input_item, current_size); current_size++; return(current_size - 1); } else { return -1; } } public int createRandomList(String string_size) { int i; int j; if(!isIntString(string_size)) { return 0; } else { Integer size = Integer.valueOf(string_size); current_size = size.intValue(); } Random R = new Random(); for(i = 0; i<current_size; i++) { j = (int)(R.nextFloat()*current_size); data.insertElementAt(Integer.toString(j), i); } return current_size; } public int createOrderedList(String size_string) { int i; if(!isIntString(size_string)) { return 0; } else { Integer size = Integer.valueOf(size_string); current_size = size.intValue(); } for(i = 0; i<current_size; i++) { data.insertElementAt(Integer.toString(i), i); } return current_size; } /* As you can see for the delete method I have tried several variations all giving the same problem, the one which isn't commented out is the one which kind of works i.e. when the delete button is pressed the correct value appears for the number of duplicates that should have been deleted but nothing visibly happens*/ public int deleteMultiple() { /* int i = 0; while(i<data.size()) { int j; int k = 0; int l = k + 1; m = 0; for(j = 0; j<data.size()-1; j++) { if(data.elementAt(k) == data.elementAt(l)) { data.removeElementAt(l); }//end if else { m++; l++; }//end else }//end for k++; i++; }//end while */ m = 0; for(int i=0; i<data.size(); i++) { for(int j=data.size()-1; j>i; j--) { if(data.elementAt(i).equals(data.elementAt(j))) { data.removeElementAt(j); m++; } } } /* int currIndex = 0; while(currIndex<data.size()) { int currIndex2; for(currIndex2 = currIndex + 1; currIndex2<data.size(); currIndex2++) { if(data.elementAt(currIndex2).equals(data.elementAt(currIndex))) { data.removeElementAt(currIndex2); currIndex2++; } } currIndex++; }*/ return m; } private boolean lessThan(String s1, String s2) { if(isIntString(s1) && isIntString(s2)) return(Integer.parseInt(s1) < Integer.parseInt(s2)); else return(s1.compareTo(s2) < 0); } private boolean isIntString(String s) { int n = s.length(); int i; if(n==0) return false; if(s.charAt(0) == '-') i = 1; else i = 0; if(i == n) return false; for(; i<n; i++) { if(s.charAt(i) < '0') return false; if(s.charAt(i) > '9') return false; } return true; } }


Reply With Quote


Bookmarks