-3

One of the things that has me stumbling for some time in interviews is the fact that their methods have List interface as a parameter and a return type. Comsider something like this:

public List<Interval> merge(List<Interval> intervals) {
  if(intervals.length()==0) return null; //does not work   
}

Above, Eclipse complains 'The method length() is undefined for the type List'. Similarly I can't iterate in a for loop using intervals.length()

I have searched this online and on other posts and I'm aware that in Java an Interface is a framework and we cannot instantiate it. But the questions says that a list of Intervals is provided in the method(data input) and I need to iterate over the List and do some merge work. How do I do that since I can't seem to even access the List. I know that in Java we can initialize a concrete class of an interface like:

List<Integer> ll = new ArrayList<>();

But doing this in the above method would lose all the existing data I get in the parameter. Another method I saw in another SO post was something like this:

if(intervals instanceOf ArrayList){
   //Do some work
 }

But obviously I cannot check for each instance that the interface can implement, can I? I mean it doesn't seem practical.

Can someone please explain how to iterate over data in a method accepts a interface/List?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
user2441441
  • 1,237
  • 4
  • 24
  • 45
  • "How do I do that since I can't seem to even access the List." Can you show what you tried and what the actual problem with it is? The point of using the interface is to be able to work with any kind of list, without requiring special code for each possible implementation. – Steven Jun 30 '17 at 03:47
  • Why can't you use the methods of List interface? – Aakash Verma Jun 30 '17 at 03:50
  • Make sure `java.util.List` is properly imported. – shmosel Jun 30 '17 at 03:51
  • You searched this online but you didn't consider consulting the Javadoc? – user207421 Jun 30 '17 at 03:59

2 Answers2

1

The first and most important part is: you are drawing wrong conclusions. You don't understand Java syntax respectively classes "good enough"; and then you "find" wrong explanations for your problems.

Lets start:

public List<Interval> merge(List<Interval> intervals) {
  if(intervals.length()==0) return null; //does not work   
}

Yes, does not work, for two reasons:

  • Arrays have a length field. But any interface/class derived from the base Collection interface has a size() method.
  • There must be a return on all paths in your method.

Taking these things together; a (syntactically) correct version of your method reads like:

public List<Interval> justReturnListIfNotEmpty(List<Interval> intervals) {
  if (intervals == null || intervals.size () == 0) {
    return null; 
  } else {
    return intervals;
  }
}

Even better, you could use intervals.isEmpty() instead. And please note: you don't have to, but it is really good practice to always use { braces } even for one line if / then / else constructs.

Next: you iterate lists via

  • the for-each loop: for(Integer bigInt : intervals) {
  • the couting for loop: for (int i=0; i < intervals.size() ...
  • using the iterator methods provided in the Collection interface

Beyond that, for the "what does it mean to use interfaces" - see here.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 2
    I think this might as well be a comment. Poster just got the wrong method and went off on a tangent about largely irrelevant things, it's the logical equivalent of a typo and unlikely to help anyone else. – pvg Jun 30 '17 at 03:55
  • @pvg That I did :) Whoops – user2441441 Jun 30 '17 at 03:56
  • Please remember that part of the question was "Can someone please explain how to iterate over data in a method accepts a interface/List?", which I don't think you addressed. – Dawood ibn Kareem Jun 30 '17 at 04:11
  • @user2441441 that happens and is totally reasonable. Comment is really aimed at the answerers than the asker - they have the greater responsibility in helping maintain the quality of the site. – pvg Jun 30 '17 at 04:52
  • @GhostCat It is and I don't think the well-intentioned effort in expanding your answer fixes it, not because the answer is bad but because I don't think the question merited that sort of answer to begin with. See the close votes and the comment by the poster. It's essentially a typo. It's hard to see how a more detailed answer can ever help anyone else - they'd never get to it on the basis of that question. – pvg Jun 30 '17 at 07:00
  • @user2441441 Minor thing: you can delete your comment now - no need to keep stuff hanging around that is actually (no longer) of value to the reader. – GhostCat Jun 30 '17 at 07:00
  • @pvg I guess you have a point there. On the other hand; there are hundreds of questions each day that are much worth. The OP put up a [mcve]; and he gave the error message. Yes, he has a syntax problem. But he also has difficulties understanding what he is doing. In that sense, the explanations could be helpful. – GhostCat Jun 30 '17 at 07:06
  • @GhostCat there are certainly many more worse questions and I don't mean to bicker with you about the details of answering or not answering a marginal question. But at 50k internet points (or at 1k or 2k) our primary criteria should be what the site guidelines repeatedly exhort - making a site that's useful to (many) other people rather than sorting out one persons's typo and related misconceptions. – pvg Jun 30 '17 at 07:23
-1

For your first problem, there is no method length() in List, it is size().

Look at this..This is how you can do it.

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        List<Integer> a= new ArrayList();
        asd(a);
    }
    static void asd(List<Integer> a){
        Iterator<Integer> it = a.iterator();
        while(it.hasNext()){
            System.out.print(it.next()); //or do some merge work
        }
    }
}
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66