Java,TreeSet。我们可以从不是第一个元素迭代元素吗?

时间:2016-05-12 15:48:32

标签: java time iterator treeset

美好的一天。

我使用TreeSet,例如TreeSet<Integer> t。 我想在x和y之间打印set t中的所有数字。

void print(Integer x,Integer y, TreeSet<Integer> t){
...
}

怎么做? 当然我可以得到t.iterator(),并且迭代所有来自集合和检查的数字是伟大的x和更少的y。 时间复杂度是y步。 但如果y-x很小。获得t.higher()会更好,之后迭代set中的元素,直到小于y。 该解决方案可以用C ++实现。 它的时间复杂度为log (t.size())+(y-x)

如何在Java中完成?

1 个答案:

答案 0 :(得分:1)

您可以使用missing module named unicodedata.ucd_3_2_0 - imported by unicodedata, stringprep, encodings.idna missing module named array.array - imported by array, httplib missing module named _warnings.warn_explicit - imported by _warnings, warnings missing module named _warnings.filters - imported by _warnings, warnings missing module named _warnings.once_registry - imported by _warnings, warnings missing module named _warnings.default_action - imported by _warnings, warnings missing module named _warnings.warn - imported by _warnings, warnings missing module named _subprocess.STD_OUTPUT_HANDLE - imported by _subprocess, subprocess missing module named _subprocess.CREATE_NEW_PROCESS_GROUP - imported by _subprocess, subprocess missing module named _subprocess.STD_INPUT_HANDLE - imported by _subprocess, subprocess missing module named _subprocess.STARTF_USESHOWWINDOW - imported by _subprocess, subprocess missing module named _subprocess.CREATE_NEW_CONSOLE - imported by _subprocess, subprocess missing module named _subprocess.STD_ERROR_HANDLE - imported by _subprocess, subprocess missing module named _subprocess.STARTF_USESTDHANDLES - imported by _subprocess, subprocess missing module named _subprocess.SW_HIDE - imported by _subprocess, subprocess missing module named cPickle.loads - imported by cPickle, collections missing module named cPickle.dumps - imported by cPickle, collections missing module named thread.get_ident - imported by thread, collections missing module named thread._local - imported by thread, threading missing module named thread.stack_size - imported by thread, threading missing module named thread.allocate_lock - imported by thread, _strptime missing module named itertools.chain - imported by itertools, heapq, collections missing module named itertools.starmap - imported by itertools, collections missing module named itertools.repeat - imported by itertools, heapq, collections missing module named itertools.ifilterfalse - imported by itertools, sets missing module named itertools.ifilter - imported by itertools, sets missing module named itertools.imap - imported by itertools, heapq missing module named itertools.tee - imported by itertools, heapq missing module named itertools.izip - imported by itertools, heapq missing module named itertools.count - imported by itertools, heapq missing module named itertools.islice - imported by itertools, repr, heapq missing module named operator.itemgetter - imported by operator, collections, heapq missing module named operator.attrgetter - imported by operator, inspect missing module named _collections.deque - imported by _collections, collections missing module named _collections.defaultdict - imported by _collections, collections missing module named _weakref.ref - imported by _weakref, _weakrefset, weakref missing module named _weakref.ReferenceType - imported by _weakref, weakref missing module named _weakref.proxy - imported by _weakref, weakref missing module named _weakref.getweakrefs - imported by _weakref, weakref missing module named _weakref.getweakrefcount - imported by _weakref, weakref missing module named _weakref.ProxyType - imported by _weakref, weakref missing module named _weakref.CallableProxyType - imported by _weakref, weakref missing module named time.time - imported by time, threading missing module named time.sleep - imported by time, threading missing module named datetime.date - imported by datetime, _strptime missing module named errno.ENOENT - imported by errno, gettext missing module named _functools.partial - imported by _functools, functools missing module named _functools.reduce - imported by _functools, functools missing module named copy.deepcopy - imported by copy, weakref, sets missing module named exceptions.ReferenceError - imported by exceptions, weakref missing module named binascii.hexlify - imported by binascii, random missing module named math.sin - imported by math, random missing module named math.sqrt - imported by math, random missing module named math.cos - imported by math, random missing module named math.acos - imported by math, random missing module named math.ceil - imported by math, random missing module named math.exp - imported by math, random missing module named math.log - imported by math, random missing module named math.e - imported by math, random missing module named math.pi - imported by math, random missing module named _ctypes.call_function - imported by _ctypes, pyreadline.console.console missing module named _ctypes._wstring_at_addr - imported by _ctypes, ctypes missing module named _ctypes._string_at_addr - imported by _ctypes, ctypes missing module named _ctypes._cast_addr - imported by _ctypes, ctypes missing module named _ctypes._memset_addr - imported by _ctypes, ctypes missing module named _ctypes._memmove_addr - imported by _ctypes, ctypes missing module named _ctypes.get_last_error - imported by _ctypes, ctypes missing module named _ctypes.set_last_error - imported by _ctypes, ctypes missing module named _ctypes._check_HRESULT - imported by _ctypes, ctypes missing module named _ctypes.set_conversion_mode - imported by _ctypes, ctypes missing module named _ctypes._pointer_type_cache - imported by _ctypes, ctypes missing module named _ctypes.POINTER - imported by _ctypes, ctypes missing module named _ctypes._SimpleCData - imported by _ctypes, ctypes missing module named _ctypes.get_errno - imported by _ctypes, ctypes missing module named _ctypes.set_errno - imported by _ctypes, ctypes missing module named _ctypes.alignment - imported by _ctypes, ctypes missing module named _ctypes.addressof - imported by _ctypes, ctypes missing module named _ctypes.resize - imported by _ctypes, ctypes missing module named _ctypes.byref - imported by _ctypes, ctypes missing module named _ctypes.sizeof - imported by _ctypes, ctypes missing module named _ctypes.dlopen - imported by _ctypes, ctypes missing module named _ctypes.FUNCFLAG_STDCALL - imported by _ctypes, ctypes missing module named _ctypes.LoadLibrary - imported by _ctypes, ctypes missing module named _ctypes.FUNCFLAG_USE_ERRNO - imported by _ctypes, ctypes missing module named _ctypes.FUNCFLAG_USE_LASTERROR - imported by _ctypes, ctypes missing module named _ctypes.FUNCFLAG_PYTHONAPI - imported by _ctypes, ctypes missing module named _ctypes.FUNCFLAG_CDECL - imported by _ctypes, ctypes missing module named _ctypes.FormatError - imported by _ctypes, ctypes missing module named _ctypes.ArgumentError - imported by _ctypes, ctypes missing module named _ctypes.RTLD_LOCAL - imported by _ctypes, ctypes missing module named _ctypes.RTLD_GLOBAL - imported by _ctypes, ctypes missing module named _ctypes.__version__ - imported by _ctypes, ctypes missing module named _ctypes.CFuncPtr - imported by _ctypes, ctypes missing module named _ctypes._Pointer - imported by _ctypes, ctypes missing module named _ctypes.Structure - imported by _ctypes, ctypes missing module named _ctypes.Array - imported by _ctypes, ctypes missing module named _ctypes.Union - imported by _ctypes, ctypes missing module named struct.calcsize - imported by struct, ctypes missing module named pyreadline.console.ansi.AnsiWriter - imported by pyreadline.console.ansi, pyreadline.console.console missing module named pyreadline.console.ansi.AnsiState - imported by pyreadline.console.ansi, pyreadline.console.console, pyreadline.console.ironpython_console missing module named pyreadline.keysyms.KeyPress - imported by pyreadline.keysyms, pyreadline.console.console missing module named pyreadline.keysyms.make_KeyPress - imported by pyreadline.keysyms, pyreadline.console.ironpython_console, pyreadline.console.console missing module named pyreadline.keysyms.make_keyinfo - imported by pyreadline.keysyms, pyreadline.console.ironpython_console missing module named pyreadline.keysyms.make_KeyPress_from_keydescr - imported by pyreadline.keysyms, pyreadline.console.ironpython_console missing module named pyreadline.keysyms.make_keysym - imported by pyreadline.keysyms, pyreadline.console.ironpython_console missing module named startup - imported by pyreadline.keysyms.common, pyreadline.keysyms.keysyms missing module named System - imported by pyreadline.clipboard.ironpython_clipboard, pyreadline.keysyms.ironpython_keysyms, pyreadline.console.ironpython_console, pyreadline.rlmain missing module named _scproxy._get_proxy_settings - imported by _scproxy, urllib `enter code here` missing module named _scproxy._get_proxies - imported by _scproxy, urllib missing module named _scproxy - imported by urllib missing module named EasyDialogs.AskPassword - imported by EasyDialogs, getpass missing module named EasyDialogs - imported by getpass missing module named termios - imported by getpass missing module named pwd - imported by posixpath, getpass missing module named SOCKS - imported by ftplib missing module named binascii.b2a_qp - imported by binascii, quopri missing module named binascii.a2b_qp - imported by binascii, quopri missing module named socket.getnameinfo - imported by socket, ssl missing module named socket.error - imported by socket, ssl missing module named socket.socket - imported by socket, ssl missing module named socket._delegate_methods - imported by socket, ssl missing module named socket._fileobject - imported by socket, ssl missing module named _ssl.PROTOCOL_SSLv2 - imported by _ssl, ssl missing module named _ssl.PROTOCOL_TLSv1 - imported by _ssl, ssl missing module named _ssl.PROTOCOL_SSLv23 - imported by _ssl, ssl missing module named _ssl.PROTOCOL_SSLv3 - imported by _ssl, ssl missing module named _ssl.SSL_ERROR_EOF - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_WANT_WRITE - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_ZERO_RETURN - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_WANT_X509_LOOKUP - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_SSL - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_WANT_CONNECT - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_INVALID_ERROR_CODE - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_WANT_READ - imported by _ssl, ssl, socket missing module named _ssl.SSL_ERROR_SYSCALL - imported by _ssl, ssl, socket missing module named _ssl.RAND_status - imported by _ssl, ssl, socket missing module named _ssl.RAND_egd - imported by _ssl, ssl, socket missing module named _ssl.RAND_add - imported by _ssl, ssl, socket missing module named _ssl.CERT_REQUIRED - imported by _ssl, ssl missing module named _ssl.CERT_NONE - imported by _ssl, ssl missing module named _ssl.CERT_OPTIONAL - imported by _ssl, ssl missing module named _ssl.SSLError - imported by _ssl, ssl, socket missing module named _ssl.OPENSSL_VERSION_NUMBER - imported by _ssl, ssl missing module named _ssl.OPENSSL_VERSION_INFO - imported by _ssl, ssl missing module named _ssl.OPENSSL_VERSION - imported by _ssl, ssl missing module named rourl2path.url2pathname - imported by rourl2path, urllib missing module named rourl2path.pathname2url - imported by rourl2path, urllib missing module named rourl2path - imported by urllib missing module named sys.py3kwarning - imported by sys, httplib missing module named binascii.b2a_base64 - imported by binascii, email.base64mime missing module named binascii.a2b_base64 - imported by binascii, email.base64mime missing module named sys.stderr - imported by sys, smtplib missing module named IronPythonConsole - imported by pyreadline.console.ironpython_console missing module named clr - imported by pyreadline.clipboard.ironpython_clipboard, pyreadline.console.ironpython_console missing module named errno.EINVAL - imported by errno, StringIO missing module named cStringIO.StringIO - imported by cStringIO, gettext, pickle, pprint, socket, email.parser, email.message, tempfile, quopri, httplib, urllib, email.generator, email.iterators, shlex, encodings.uu_codec, encodings.quopri_codec missing module named binascii.a2b_uu - imported by binascii, encodings.uu_codec missing module named binascii.b2a_uu - imported by binascii, encodings.uu_codec missing module named 'org.python'.PyStringMap - imported by 'org.python', pickle missing module named 'org.python' - imported by pickle missing module named _struct.__doc__ - imported by _struct, struct missing module named _struct._clearcache - imported by _struct, struct missing module named fcntl - imported by subprocess, tempfile missing module named threading.RLock - imported by threading, _threading_local missing module named threading.current_thread - imported by threading, _threading_local missing module named traceback.format_exc - imported by traceback, threading missing module named os.urandom - imported by os, random missing module named riscosenviron._Environ - imported by riscosenviron, os missing module named riscosenviron - imported by os missing module named riscospath - imported by os missing module named riscos._exit - imported by riscos, os missing module named riscos - imported by os missing module named ce._exit - imported by ce, os missing module named ce - imported by os missing module named _emx_link.link - imported by _emx_link, os missing module named _emx_link - imported by os missing module named os2._exit - imported by os2, os missing module named os2 - imported by os missing module named nt._isdir - imported by nt, ntpath missing module named nt._getfullpathname - imported by nt, ntpath missing module named nt._exit - imported by nt, os missing module named posix._exit - imported by posix, os missing module named posix - imported by os missing module named resource - imported by posix missing module named strop.whitespace - imported by strop, string missing module named strop.maketrans - imported by strop, string missing module named strop.lowercase - imported by strop, string missing module named strop.uppercase - imported by strop, string missing module named org.PyStringMap - imported by org, copy missing module named org - imported by copy missing module named re._compile - imported by re, sre missing module named re.__all__ - imported by re, sre missing module named codecs.mbcs_encode - imported by codecs, encodings.mbcs missing module named codecs.mbcs_decode - imported by codecs, encodings.mbcs 接口(SortedSet实现)。例如:

TreeSet

输出:

SortedSet<Integer> set = new TreeSet<Integer>();

for (int i = 1; i <= 5; ++i) {
    set.add(i);
}

int from = 2;
int to = 4;
for (int x : set.subSet(from, to+1)) { // Note that the higher bound is exclusive.
    System.out.println(x);
}