java - How to use AbstractList -
I'm new to Java, just started it a while ago. I am trying to create chat messenger (client-server) and I need a data structure in which I can store an online user (a socket and a string that contains the username). Now I did a lot of research and thought that the AbstraList can help me, but I have to face a problem
public class collection & lt; E & gt; Abstracts & lt; E & gt; {Private integer size; Private e [] list; Public collection () {size = 0; List = new E [50]; } @ Override public e-mail (int index) {return list [index]; } @ Override Public Boolean Aid (E element) {return false; } @ Override Public Int size () {return list.length; } Public static zero main (string [] args) {Collection < String & gt; Obj = new collection (); Obj.add (new string ("1")); Obj.add (new string ("5"); System.out.println ("size =" + obj.size ()); }} This is my code and clearly I could not find a way around the add function ... I
list = new E Tried to use [50]; But this gives a normal initialization error. In short, I do not know how to initialize the inventory object. Now I have three questions:
- Is the server Is my approach to making a list of all customers on?
- What should I do with the add () function?
- Why are there two times in the first row? Obviously this is a template, but will not you need it once? Obviously you need both places, so what's the first and what is the other for? (The tutorial that I have learned is not considered silent)
to add items A list you can make, you do not need to create your own list class
list & lt; String & gt; MyList = new Arsèchchi & lt; String & gt; (); MyList.add ("Peter"); MyList.add ("Susan"); Now if you want to make a list of all usernames and sockets, you can use the map. Something like this
Maps & lt; String, socket & gt; MyMap = New Hashmop & lt; String, socket & gt; (); MyMap.put ("Peter", Peterrskete); MyMap.put ("Susan", Sense Socket); This will allow you to get a socket with the user names
socket pies = myMap.get ("Peter"); Or you can create your username and socket class instead
public class usernameNumberPoint {the name of the private string; Private socket socket; Public user namespace (string name, socket socket) {this.name = name; this. Socket = socket; } Public string getName () {return name; } Public Socket Socket () {Return Socket; }} Then it can be added to the list
list & lt; UsernameSocketPair & gt; MyList = New Arrestist & lt; Username SocketPayer & gt; (); MyList.add (new user nameSocketPear ("Peter", Patrssocket)); Whether this is a good idea or not, it depends on the rest of your code and how it is structured.
The answers to the questions clearly:
- It depends on the rest of your code and how it is structured.
- Do not use the first [E] [object] [] and put it on E when you get the 'element' in this way you instantiate (such as object [] list = new object [20]; < In the class declaration, e is happening twice because you are extending the type of abstracts and you are doing parameters by your class. For example, you can do this.
The following:
Public class MyList AbstractList {} N How to apply a solid version of a table list, as you wanted
Public category MyList & lt; E & gt; Abstracts & lt; E & gt; {Private Item [ ] = New object [10]; Private int size = 0, public e-mill (int i) {if (i> = size) new index auto offbound exception ("duh!"); Return (e) list [i] ;} Public Boolean addition (EE) {if (size> gt; = list.length} {object [new] newList = new object [list.length + 10]; System.arraycopy (list, 0, newList, 0, list .length); List = newList;} list [size] = E; Size ++; True true;} public integer size () {return size;} public static zero main (string [] arg s) {list & lt; String & gt; L = New MyList & lt; String & gt; (); For (int i = 0; i & lt; 100; i ++) {l.add ("" + i); } System.out.println (l.size ()); }}
Algolia is the fastest and most relevant search experience available for Opencart. Algolia helps Opencart stores connect their users with what matters most via a search which delivers lightning-fast, relevant search results anywhere in the world.
ReplyDeletePurchasetheme is one kind of platform for the developers and designers. Whoever feels important can buy and sell the readymade work related to web development assets and print comprising scripts, codes, themes, banners, logos, graphics, plugins as well as mobile or desktop app source codes.What is semantic search? It's the technology that uses artificial intelligence (AI) to comprehend the user's intent behind every search query and also the context of the query.So, even if the user types in a query with minor typos or spelling errors, it tries to get into the mind of the user to understand what he was probably thinking and what results he seeks. Opencart Search Extension