delete replicating values from an array[]....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aswath
    New Member
    • Mar 2008
    • 18

    delete replicating values from an array[]....

    hi dere,
    i have a question.
    i have an array with replicating values.... al i want to do is
    delete all replicating values and store the result in another array.
    eg.,
    String[] a={1,2,3,3};
    String []b;
    b should be 1.2.3only...... ..not 1,2,3,3
    thanks in advance,
    regards,
    aswath.n.s
    (knowledge is power)
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by aswath
    hi dere,
    i have a question.
    i have an array with replicating values.... al i want to do is
    delete all replicating values and store the result in another array.
    eg.,
    String[] a={1,2,3,3};
    String []b;
    b should be 1.2.3only...... ..not 1,2,3,3
    thanks in advance,
    regards,
    aswath.n.s
    (knowledge is power)
    What is your question?!

    regards,
    sukatoa

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by aswath
      hi dere,
      i have a question.
      i have an array with replicating values.... al i want to do is
      delete all replicating values and store the result in another array.
      eg.,
      String[] a={1,2,3,3};
      String []b;
      b should be 1.2.3only...... ..not 1,2,3,3
      thanks in advance,
      regards,
      aswath.n.s
      (knowledge is power)
      ... and what have you done so far?

      Kind regards

      HashSet.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by aswath
        (knowledge is power)
        I don't think so: knowing what to do with your knowledge may give some power,
        otherwise you just have to carry that knowledge around in your brain.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          I don't think so: knowing what to do with your knowledge may give some power,
          otherwise you just have to carry that knowledge around in your brain.

          kind regards,

          Jos
          ... and to prove that, I wrote some VB.NET code today.(there's a first time for everything).
          @Jos surely there's an easier way for that problem?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by r035198x
            ... and to prove that, I wrote some VB.NET code today.(there's a first time for everything).
            @Jos surely there's an easier way for that problem?
            Erm, I didn't mean to say that when you accidentally know something that you
            *have* to exercise that knowledge. Some knowledge can better be forgotten ;-)

            kind regards,

            Jos

            ps. you can also iterate from the right and stop when you hit a letter ...

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              ...

              ps. you can also iterate from the right and stop when you hit a letter ...
              Yeah I knew that, I just couldn't grasp the syntax for the for loop!!

              *hides in a little corner*

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                Yeah I knew that, I just couldn't grasp the syntax for the for loop!!
                I consider that a little plus on your side.

                Originally posted by r035198x
                *hides in a little corner*
                No need to, nobody is perfect. Case closed, this is a Java forum after all.

                kind regards,

                Jos ( <--- knows nooooothing about Basic ;-)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  I consider that a little plus on your side.



                  No need to, nobody is perfect. Case closed, this is a Java forum after all.

                  kind regards,

                  Jos ( <--- knows nooooothing about Basic ;-)
                  Coming back to the original question then, how would I get this duplicates Snipper to work for primitives as well?


                  [CODE=java]import java.util.Array s;
                  import java.util.Colle ction;
                  import java.util.HashS et;
                  public class Snipper<E> {
                  E[] array;
                  public static void main(String[] args) {
                  Integer[] x = {4,4,7};
                  System.out.prin tln(Arrays.toSt ring(new Snipper<Integer >().removeDups( x)));
                  }
                  public E[] removeDups(Obje ct o) {
                  Collection<E> c = null;

                  if(o instanceof Collection) {
                  c = (Collection<E>) o;
                  }
                  else {
                  try {
                  array = (E[])o;
                  }
                  catch(ClassCast Exception cCE) {
                  System.out.prin tln("Not supported");

                  }
                  }
                  if(c != null) {
                  HashSet<E> s = new HashSet (c);
                  array = (E[])s.toArray();
                  }
                  else if(array != null) {
                  final HashSet<E> s = new HashSet () {
                  {
                  for(E e : array) {
                  add(e);
                  }
                  }
                  };
                  array = (E[])s.toArray();
                  }
                  return array;
                  }
                  }[/CODE]

                  Comment

                  • lopez
                    New Member
                    • Mar 2008
                    • 6

                    #10
                    Originally posted by r035198x
                    Coming back to the original question then, how would I get this duplicates Snipper to work for primitives as well?


                    [CODE=java]import java.util.Array s;
                    import java.util.Colle ction;
                    import java.util.HashS et;
                    public class Snipper<E> {
                    E[] array;
                    public static void main(String[] args) {
                    Integer[] x = {4,4,7};
                    System.out.prin tln(Arrays.toSt ring(new Snipper<Integer >().removeDups( x)));
                    }
                    public E[] removeDups(Obje ct o) {
                    Collection<E> c = null;

                    if(o instanceof Collection) {
                    c = (Collection<E>) o;
                    }
                    else {
                    try {
                    array = (E[])o;
                    }
                    catch(ClassCast Exception cCE) {
                    System.out.prin tln("Not supported");

                    }
                    }
                    if(c != null) {
                    HashSet<E> s = new HashSet (c);
                    array = (E[])s.toArray();
                    }
                    else if(array != null) {
                    final HashSet<E> s = new HashSet () {
                    {
                    for(E e : array) {
                    add(e);
                    }
                    }
                    };
                    array = (E[])s.toArray();
                    }
                    return array;
                    }
                    }[/CODE]


                    come on frds.....
                    i said knowledge is power not MY knowledge is powerful...
                    n'way lets drop this issue...
                    hi r035198x ,
                    i used ur code and its working fine.....
                    however i would relly appricate if u could explain me that... i hope i'm not bothering u...
                    regards,
                    aswath ns
                    (knowledge is power)

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by lopez
                      come on frds.....
                      i said knowledge is power not MY knowledge is powerful...
                      n'way lets drop this issue...
                      hi r035198x ,
                      i used ur code and its working fine.....
                      however i would relly appricate if u could explain me that... i hope i'm not bothering u...
                      regards,
                      aswath ns
                      (knowledge is power)
                      I didn't think you were going to use it because I thought you had an int[].
                      Which part do you not understand?

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Hey, are you using two accounts?

                        Comment

                        • aswath
                          New Member
                          • Mar 2008
                          • 18

                          #13
                          Originally posted by r035198x
                          Hey, are you using two accounts?

                          she is my college Colleague.. check her mail id if needed frnd.. i posed from her account...
                          i cannot get u wen u say<E>.. is that some generic terms r wat.. can u plz help me with that.....
                          regards

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by aswath
                            she is my college Colleague.. check her mail id if needed frnd.. i posed from her account...
                            i cannot get u wen u say<E>.. is that some generic terms r wat.. can u plz help me with that.....
                            regards
                            You can read all about those here.

                            Comment

                            • prisesh26
                              New Member
                              • Sep 2006
                              • 29

                              #15
                              wat happened to this forum..
                              why the Admin's are very hard in their words.

                              please understand the other people those who are starter, freshers to this language and selected this forum for help.

                              why cant the experienced people know and understand this and reply to your
                              help needed people.

                              no one knows fully and no one is unknown here...

                              please dont use hard words which will make the subscribers not to visit again


                              this forum helped me so much..

                              so i wrote this reply.

                              Even though this is not the JAVA RELATED REPLY.


                              regards!!!
                              well wisher

                              Comment

                              Working...