Java 在没有LinkedList的情况下执行此操作的其他方法?
我的IDE建议我使用LinkedList,因此它会自动将其导入到我的代码中。但是,有没有一种方法可以实现同样的效果,但不必导入LinkedList而只使用ArrayList?显然这是可能的,但我不知道怎么做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
/**
* 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语言在生产率和可读性方面的一个很好的补充。您可以从这里阅读更多内容: