i have an array like this:

String[][] data= {"s", "w","s", "y", "r", "r", "y", "p"};

i wanna do some action if there was no similar array element before that particular element,
for example, do something if data[0]=="s", then do something if data[1] == "w", but skip when data[2]=="s", because "s" already exists before data[2], like this:

for(int i=0; i<data.length; i++){

  if(data[i] != SIMILAR ELEMENT BEFORE data[i]){

      DO SOME ACTION
  }
}

can someone please tell me some idea? please...
thank u

Recommended Answers

All 7 Replies

I would write a helper method, call it something like foundPrevious , that takes your array, an index, and a string. It's a boolean method that searches the array from index 0 up to the input index searching for the input string. If it finds the string at any point, it returns true. Otherwise it returns false. Then you can use this method in your if condition on line 3.

and

String[][] data = {{"", "", ""}, {"", "", ""}, {"", "", ""}, {"", "", ""}, {"", "", ""}, {"", "", ""}}

or

String [] data= {"", "","", "", "", "", "", ""};

are you kidding, if not then you have to **** which one, and then we can play with magic previous Element

and

String[][] data = {{"", "", ""}, {"", "", ""}, {"", "", ""}, {"", "", ""}, {"", "", ""}, {"", "", ""}}

or

String [] data= {"", "","", "", "", "", "", ""};

are you kidding, if not then you have to **** which one, and then we can play with magic previous Element

oops, that's my mistake :P

The first thing that comes to my mind for this is just to make an ArrayList of Strings. If the string isn't in the list (use the contains method), add it to the list and do whatever you do the first time you see an element. If it is in the list, you've seen it before, so do your second-time thing.

public class findSth {

    String [] array = {"r", "e", "t", "r", "k", "t", "r", "k"};

    public boolean findPrevious(int dataIdx, String data, String[] array){

        boolean found = false;

            for(int i=0; i<dataIdx; i++){
                if(array[i] == data){
                    found = true;
                    break;
                }else if (array[i] != data){
                    found = false;
                    break;
                }
            }

       return found;

    }

    public static void main(String args[]){

        findSth o = new findSth();

        for(int i=0; i<o.array.length; i++){
            if(o.findPrevious(i, o.array[i], o.array))
                System.out.println("found: "+o.array[i]);
            else
                System.out.println("not found: "+o.array[i]);
        }
    }

}

i wrote this code, but it produces this result:

not found: r
not found: e
not found: t
found: r
not found: k
not found: t
found: r
not found: k

while the desired result is:
not found: r
not found: e
not found: t
found: r
not found: k
found: t
found: r
found: k

i dont understand what is the problem :(

i--

for (int i = array.length; i > 0; i--){

ha! got it!
just remove the break; after found = false;
yay! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.