public Collection<Comment> getCommentCollection() {
commentCollection = movie.getCommentCollection();
return split((List<Comment>) commentCollection, 4);
}
public Collection<Comment> split(List<Comment> list, int size){
int numBatches = (list.size() / size) + 1;
Collection[] batches = new Collection[numBatches];
Collection<Comment> set = commentCollection;
for(int index = 0; index < numBatches; index++) {
int count = index + 1;
int fromIndex = Math.max(((count - 1) * size), 0);
int toIndex = Math.min((count * size), list.size());
batches[index] = list.subList(fromIndex, toIndex);
set = batches[index];
}
return set;
}
Run Code Online (Sandbox Code Playgroud)
嗨,我试图将一个集合划分为更小的集合,这取决于"母亲"集合中的项目数量,并在每次调用get方法时返回其中一个较小的集合(跟踪它是哪一个),有人可以帮我一把吗?
非常感谢你.
伊格纳西奥
private List<Movie> movieItems = null;
public List<Movie> getMovieItems() {
final int first = 0;
if (movieItems == null) {
getPagingInfo();
movieItems = jpaController.findRange(new int[]{pagingInfo.getFirstItem(), pagingInfo.getFirstItem() + pagingInfo.getBatchSize()});
Collections.sort(movieItems, new Comparator(){
public int compare (Object o1, Object o2){
Date d1 = movieItems.get(((Movie)o1).getMovieId()).getDate();
Date d2 = movieItems.get(((Movie)o2).getMovieId()).getDate();
if(d1.before(d2)){
movieItems.set(1, (Movie)o1);
movieItems.set(2, (Movie)o2);
}
return first;
}
});
}
return movieItems;
}
Run Code Online (Sandbox Code Playgroud)
jpaController带回了4部电影,给我以下内容
在java.util.Arrays中在java.util.Vector.get(Vector.java:694)在entitybeans.jsf.PeliculaController $ 1.compare(PeliculaController.java:260)4:java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围.mergeSort(Arrays.java:1270)在java.util.Arrays.sort(Arrays.java:1210)在java.util.Collections.sort(Collections.java:159)在entitybeans.jsf.PeliculaController.getPeliculaItems(PeliculaController.的java:257)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在java.lang中.reflect.Method.invoke(Method.java:597)在javax.el.BeanELResolver.getValue(BeanELResolver.java:302)在javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)在com.sun.faces. el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)在com.sun.el.parser.AstValue.getValue(AstValue.java:116)在com.sun.el.parser.A stValue.getValue(AstValue.java:163)....
嗨,我想在JavaScript中实现观察者模式:
我的index.js:
$(document).ready(function () {
var ironMan = new Movie();
ironMan.setTitle('IronMan');
ironMan.setRating('R');
ironMan.setId(1);
// ironMan.setCast(['Robert Downey Jr.', 'Jeff Bridges', 'Gwyneth Paltrow']);
var terminator = new Movie();
terminator.setTitle('Terminator');
terminator.setRating('P');
terminator.setId(2);
console.log(ironMan.toString());
console.log(terminator.toString());
ironMan.play();
ironMan.stop();
ironMan.download();
ironMan.share('V. Rivas');
console.log(ironMan.getCast()[0]);
});
Run Code Online (Sandbox Code Playgroud)
我的电影:
var title;
var rating;
var id;
var observers;
function Movie() {
observers = new ObserverList();
}
//function Movie (title, rating, id){
// this. title = title;
// this.rating = rating;
// this.id =id;
// observers = new ObserverList();
//} …
Run Code Online (Sandbox Code Playgroud) 使用DefaultHttpClient()就可以了
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url_login.toURI());
HttpGet request1 = new HttpGet(url_getList.toURI());
HttpGet request2 = new HttpGet(url.getOtherList.toURI());
HttpResponse response = client.execute(request);
HttpResponse response1 = client.execute(request1);
HttpResponse response2 = client.execute(request2);
Run Code Online (Sandbox Code Playgroud)
并且客户端会保留会话,如何通过改造2来管理以下内容
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Customize the request
Request request = original.newBuilder()
.header("Accept", "application/json")
.header("Authorization", "auth-token")
.method(original.method(), original.body())
.build();
Response response = chain.proceed(request);
// Customize …
Run Code Online (Sandbox Code Playgroud) public List<Movie> findRange(int[] range) {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Movie.class));
Query q = em.createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
List<Movie> list1 = q.getResultList();
Collections.sort(list1, new Comparator(){
public int compare (Object o1, Object o2){
Movie p1 = (Movie)o1;
Movie p2 = (Movie)o2;
return p2.getDate().compareTo(p1.getDate());
}
});
return list1;
}
Run Code Online (Sandbox Code Playgroud)
因为它现在正在排序工作,但只在批处理中,
(批处理= 4部电影)第一部分=最后输入; 最后一个=输入fisrt.
但只与批次中的那些进行比较而不是根据需要进行全部比较.
非常感谢您提供任何帮助,您可以提供
最好的问候
Ignacio
如果我有:
int a = 123;
int b = 456;
Run Code Online (Sandbox Code Playgroud)
如何得到?:
double c = 123.456;
Run Code Online (Sandbox Code Playgroud) 我已经实现了一个加载新闻的ListView,但是当我滚动列表时,新闻会改变位置.这是清单
public class ListNewsFragment extends SherlockListFragment{
private ListNewsAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
mAdapter = new ListNewsAdapter(this, app.getAllNews());
setListAdapter(mAdapter);
super.onCreate(savedInstanceState);
}
}
public class ListNewsAdapter extends BaseAdapter{
private List<News> news;
private Context mContext;
private LayoutInflater inflater;
private ViewHolder holder;
public ListNewsAdapter(final Fragment c, List<News> news) {
super();
this.news = news;
this.mContext = c.getActivity();
inflater = LayoutInflater.from(mContext);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
inflater = …
Run Code Online (Sandbox Code Playgroud) java ×4
collections ×3
android ×2
arrays ×1
javascript ×1
jpa ×1
list ×1
listview ×1
retrofit ×1
sorting ×1