954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to check previous array elements?

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

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 
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 :(

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

i--

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: