0

Okay,so i wanted to find the largest sub-string in a given string in java. So, i went ahead and wrote a recursive function for the same to identify and return the size of each sub string. My initial plan was to have all sizes of the sub-string added to Array list. I hen proposed to sort the array list in descending order and taking the top most value which would give me the size of the Largest sub-string. The problem is my ArrayList wen bonkers. Its not adding the values my function is returning but storing some other values.

My function is returning: 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 2 0 1

But what is stored is : 0 18 17 -1 -1 -1 -1 -1 11 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

As you can this is totally different . I did figure out a solution but i want to know why ArrayList is not working as it should. Any insight will be greatly appreciated. Again i just want to know why the ArrayList is not working as it should.

package sush;
import java.util.ArrayList;
import java.util.Collections;

public class Paliandrome {

static int chk ( String s ,int x, int y,int count)
{
    String begins=s.charAt(x)+"";
    String end=s.charAt(y)+"";
    if(x==y && begins.equals(end))
    {
        System.out.println(count);
        return count+1;
    }
    if(x>y)
        return count;
    if(begins.equals(end))
        return chk(s,x+1,y-1,count+2);
    else
        return count=0;
}


public static void main(String[] args) 
{

    String sh = "aabaabaasdwaasddsaa";
    int x = 0 , y = 0;
    int count=0,high=0;
    ArrayList<Integer> arr = new ArrayList<Integer>();
    arr.clear();
    for(int i=0 ; i<sh.length() ; i++ )
    {
        x=i;
        y=sh.length()-1;
        count=0;
        int temp =chk(sh,x,y,count);
        System.out.println(temp);
        arr.add(temp);

    }     
    System.out.println(high+"\n"+"ArrayList contents :");
    for(int i= 0;i<arr.size();i++)
    {
        System.out.println(arr.indexOf(i));
    }
    Collections.sort(arr);
    Collections.reverse(arr);
    System.out.println(arr.indexOf(0));
}

}

2 Answers 2

2

You are using arr.indexOf(x) when you should be using arr.get(index).

List.indexOf(x) returns the first index of a given value in the list. -1 means that x isn't in the list.

On the other hand arr.get(index) returns the element at the given index.

Sign up to request clarification or add additional context in comments.

1 Comment

your totally right.Totally noob mistake at my end , i'm just learning java. Thank you!
0

The arraylist is working fine; the problem is that you're not using the right method to retrieve and print its elements:

    System.out.println(arr.indexOf(i));

ArrayList.indexOf(E) searches the array for E and returns the location of element E, or -1 if there is no such element. To retrieve elements from the array, as you want to do, use ArrayList.get(int) to retrieve the element at the specified position.

    System.out.println(arr.get(i));

Or, even better, use the ArrayList's iterator via an enhanced for loop:

for(int num : arr)
{
    System.out.println(num);
}

1 Comment

Thanks for the tip!. I dont knw anything about iterator or enhanced for loop, Ill definitely look into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.