LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2003, 06:53 PM   #1
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Rep: Reputation: 45
ArrayList question (Java)


I am trying to write a code where values are read via commandline arguments and stored as strings in an ArrayList (with no duplicates).
Code:
public static void main(String argv[]) throws IOException {
    BufferedReader br = new BufferedReader(
               new InputStreamReader(System.in));
    String courseName;
    ArrayList names= new ArrayList();
    String s= br.readLine();
    while( s != null) {
      StringTokenizer st= new StringTokenizer(s);

      while (st.hasMoreElements()) {
          courseName= st.nextToken();
          A3 a= new A3(courseName, numPrereq);
          if (!names.contains(a)) names.add(a);
          else continue;
      }
        s= br.readLine();
      }
But, the output I get contains duplicates...can someone point to me what I am doing wrong?
 
Old 10-14-2003, 03:05 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
I tried your sample code adding a simple String to the ArrayList (because I don't know what object A3 does), and it works... No duplicates are added to the ArrayList.

Is it possible that the A3 object makes them different?

Last edited by nephilim; 10-14-2003 at 03:08 AM.
 
Old 10-14-2003, 09:38 AM   #3
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Original Poster
Rep: Reputation: 45
here is the complete code:
Code:
 
public static void main(String argv[]) throws IOException {
    BufferedReader br = new BufferedReader(
               new InputStreamReader(System.in));
    String courseName;
    ArrayList names= new ArrayList();
    String s= br.readLine();
    while( s != null) {
      StringTokenizer st= new StringTokenizer(s);

      while (st.hasMoreElements()) {
          courseName= st.nextToken();
          A3 a= new A3(courseName);
          if (!names.contains(a)) names.add(a);
          else continue;
      }
        s= br.readLine();
      }
                                                                                
   for (int num= 0; num< names.size(); num++) {
   System.out.println(names.get(num));
  }
 }
}
Testdata:
CS1101 CS1100
Ma1010 Ma1000
CS2132 CS1100
CS3132 CS2132
CS3132 CS2140

here is the output that I get:
CS1101:
CS1100:
Ma1010:
Ma1000:
CS2132:
CS1100:
CS3132:
CS2132:
CS3132:
CS2140:
 
Old 10-14-2003, 09:39 AM   #4
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Original Poster
Rep: Reputation: 45
this is the code for main class:
Code:
import java.util.*;
import java.io.*;
public class A3 {
  String course;  //name of the course
                                                                                
  public A3(String s) { // class constructor
  course= s;
  }
  public String toString() {  // println method
        return course+ ": "  ;
 }
Sorry I made a mistake in copy pasting in my earlier post
 
Old 10-14-2003, 10:03 AM   #5
glj
Member
 
Registered: Jul 2001
Location: London
Distribution: RH 9
Posts: 151

Rep: Reputation: 30
I'm not eintrely sure if this is true but maybe because you've created instances of A3 even though they may have the same attributes, they are seen as different because they are two different and distinct instances.
Maybe you should test for equality before creating an instance of A3 i.e. check if courseName exists in names, and then decide on whether or not to create a new A3.

Does that make sense? :S

glj
 
Old 10-14-2003, 02:00 PM   #6
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Original Poster
Rep: Reputation: 45
yea..i think it makes sense glj. I will give it a try
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Question k1ll3r_x Programming 2 08-20-2005 08:10 PM
java question zaicheke Programming 5 02-15-2005 12:40 PM
Java Question k1ll3r_x Programming 3 11-13-2004 04:27 AM
Java ArrayList question ksgill Programming 2 10-12-2003 07:54 AM
java question TommyD Linux - Newbie 4 07-19-2003 04:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration