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

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.

Edited 5 Years Ago by kramerd: n/a

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 :(

Edited 5 Years Ago by gingerfish: adding more info

This question has already been answered. Start a new discussion instead.