Help with arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Help with arrays

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    Arrow Help with arrays

    Hi what i am having problems with arrays.
    I have an array of, for now 10 string objects, and a second array containing the same objects. What i want to do is compare them and anywhere an object in the first array appears more than once in the second array i want to remove, it so that each object only occurs one time in the second array.
    I have an idea how this could be done, take a for loop which iterates through all elements in the first array, and inside that a second loop that takes the first element of that array and compares it to each element in the second array, and so on for each element in the array. I just dont know how to implement this in java, or how to remove the duplicate entries from the second array, can anyone help it would be greatly appreciated!
    Thanks

    Just if this is any help, what i am doing at the moment is this:

    for(int i = 0, n = words.length; i<n; i++) {
    totalWords++;
    for(int j = i+1, m = array1.length; j < m; j++){
    if(words[i].equals(array1[j])){
    System.out.println("match :" + words[i]);

    Since i dont know how to remove an element from the array i have just been printing out where a duplicate word is found, however if a word appears more than 3 times in the array then it outputs "match" for that word an incorrect number of times... can anybody see why?
    Last edited by cupanTae; 01-06-2006 at 02:38 PM.

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    It would be alot easier to use ArrayLists if you need to remove elements. Otherwise you would have to create a new array each time you remove an element. You could use a method like this if you really need to use regular arrays...(homework??)

    Code:
    public static String [] remove(String [] arr, int index){
    		if(index < 0 || index >= arr.length){
    			return null;
    		}
    		String [] arr2 = new String[arr.length - 1];
    		for(int i = 0, j = 0; i < arr.length && j < arr2.length; ++i){
    			if(i != index){
    				arr2[j++] = arr[i];
    			}
    		}
    		return arr2;
    	}

  3. #3
    Join Date
    Oct 2005
    Posts
    16
    Thanks Joe
    No its not homework its a project im working on just not very familiar with array lists i tried using them originally but again didnt know how to remove an element from a list

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Would it make sense to only add to the second array if you do not find the current element in the first array in the second array? Here is pseudocode for an possible approach to this

    Code:
    foreach ( element : array1 )
    {
         search array2 for array1.currentElement;
         if (notFound)
         {
              array2.add( array1.currentElement );
         }
    }

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Quote Originally Posted by Joe Beam
    It would be alot easier to use ArrayLists if you need to remove elements.
    Definitely, whenever you are dealing with an object/valuegroup of which you don't know how many you will be getting, then Java is packed with tools for handling that (vector, arraylist, set...). Using arrays for this is reserved for those who enjoy pain and those who have to do it as part of their education.
    eschew obfuscation

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    And all it takes to remove an element of an ArrayList is to use the list's remove( index) method.

    It should be worth your investment of time into reading the API documentation for ArrayList and to work through the tutorials on the java.sun.com web site, especially the Collections Framework materials.
    Last edited by nspils; 01-07-2006 at 12:51 PM.

Similar Threads

  1. Control Arrays in VB.NET
    By Gary Nelson in forum .NET
    Replies: 277
    Last Post: 10-01-2003, 01:00 AM
  2. Copy/Duplicate Arrays
    By Steve Cochran in forum .NET
    Replies: 8
    Last Post: 10-18-2002, 01:10 PM
  3. Replies: 15
    Last Post: 05-09-2001, 05:40 AM
  4. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 04:06 PM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 0
    Last Post: 06-20-2000, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center