Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在没有LinkedList的情况下执行此操作的其他方法?_Java_Algorithm_Arraylist_Linked List - Fatal编程技术网

Java 在没有LinkedList的情况下执行此操作的其他方法?

Java 在没有LinkedList的情况下执行此操作的其他方法?,java,algorithm,arraylist,linked-list,Java,Algorithm,Arraylist,Linked List,我的IDE建议我使用LinkedList,因此它会自动将其导入到我的代码中。但是,有没有一种方法可以实现同样的效果,但不必导入LinkedList而只使用ArrayList?显然这是可能的,但我不知道怎么做 /** * Search this Twitter for the latest Tweet of a given author. * If there are no tweets from the given author, then the * method returns nul

我的IDE建议我使用LinkedList,因此它会自动将其导入到我的代码中。但是,有没有一种方法可以实现同样的效果,但不必导入LinkedList而只使用ArrayList?显然这是可能的,但我不知道怎么做

/**
 * Search this Twitter for the latest Tweet of a given author.
 * If there are no tweets from the given author, then the
 * method returns null.
 * O(1)
 */
public Tweet latestTweetByAuthor(String author) {
    //getting hashvalue for the date
    int hashValue = tweetsByAuthor2.hashFunction(author);

    //getting all the hashtable buckets
    ArrayList<LinkedList<HashPair<String, Tweet>>> allBuckets = tweetsByAuthor2.getBuckets();

    //if buckets at hashvalue == null
    if (allBuckets.get(hashValue) == null) {
        return null;
    } else {
        return allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1).getValue();
    }
}
/**
*在此Twitter上搜索给定作者的最新推文。
*如果没有来自给定作者的推文,则
*方法返回null。
*O(1)
*/
公共推文latestTweetByAuthor(字符串作者){
//正在获取日期的哈希值
int hashValue=tweetsByAuthor2.hashFunction(作者);
//获取所有哈希表存储桶
ArrayList allbucket=tweetsByAuthor2.getbucket();
//如果hashvalue==null处的bucket
if(allbucket.get(hashValue)==null){
返回null;
}否则{
返回allbucket.get(hashValue).get(allbucket.get(hashValue).size()-1.getValue();
}
}
您可以试试这个

   public Tweet latestTweetByAuthor(String author) {
        // getting hashvalue for the date
        int hashValue = tweetsByAuthor2.hashFunction(author);

        // getting all the hashtable buckets
        ArrayList<ArrayList<HashPair<String, Tweet>>> allBuckets = tweetsByAuthor2.getBuckets();

        // if buckets at hashvalue == null
        if (allBuckets.get(hashValue) == null) {
            return null;
        } else {
            return allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1).getValue();
        }
    }
公共推文最新推文作者(字符串作者){
//正在获取日期的哈希值
int hashValue=tweetsByAuthor2.hashFunction(作者);
//获取所有哈希表存储桶
ArrayList allbucket=tweetsByAuthor2.getbucket();
//如果hashvalue==null处的bucket
if(allbucket.get(hashValue)==null){
返回null;
}否则{
返回allbucket.get(hashValue).get(allbucket.get(hashValue).size()-1.getValue();
}
}

您可以,但是您必须将您的
tweetsByAuthor2.getBucket()
返回类型也更改为
ArrayList stackoverflow answer,以便将接口用作返回类型

如果您使用的是Java 10或更高版本,为了清晰起见,您可以对复杂变量类型使用关键字

/**
 * Search this Twitter for the latest Tweet of a given author.
 * If there are no tweets from the given author, then the
 * method returns null.
 * O(1)
 */
public Tweet latestTweetByAuthor(String author) {
    //getting hashvalue for the date
    int hashValue = tweetsByAuthor2.hashFunction(author);

    //getting all the hashtable buckets
    var allBuckets = tweetsByAuthor2.getBuckets();

    //if buckets at hashvalue == null
    if (allBuckets.get(hashValue) == null) {
        return null;
    } else {
        return allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1).getValue();
    }
}

因此
getbucket()
的实现返回
arraylisties,在另一个类中,我有私有的ArrayList bucket;使用var工作正常,我可以从java文件中删除linkedList导入。您能解释一下为什么这样做吗?var是Java语言在生产率和可读性方面的一个很好的补充。您可以从这里阅读更多内容: