Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify an array of int types in JAVA to be generic

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to modify the array of int types in JAVA to generics". In the daily operation, I believe that many people have doubts about how to modify the array of int types into generics in JAVA. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to modify the array of int types in JAVA to generics". Next, please follow the editor to study!

Int type array

Let's start with a simple content. This is normal. I think I can sometimes meet in daily development, ha, whether it is normal or not depends on whether I have used it or not.

Public class Main {

Public static void main (String [] args) {

Array score=new Array (10)

For (int iTuno witi size) {

Throw new IllegalArgumentException ("Add is fail.Require index > = 0 and index)

< size"); } for (int i = size; i >

Index; iMurt -) {

Data [I] = data [I-1]

}

Data [index] = e

Size++

}

/ * *

* get the elements of the index location

* through this encapsulation, users cannot query unused space, which ensures the security of the data.

*

* @ param index

* @ return

, /

Public T get (int index) {

If (index

< 0 || index >

= size) {

Throw new IllegalArgumentException ("Get is failed.Index is illegal")

}

Return data [index]

}

/ * *

* modify the elements of the index location

*

* @ param index

* @ param e

, /

Public void set (int index, te) {

If (index

< 0 || index >

= size) {

Throw new IllegalArgumentException ("Set is failed.Index is illegal")

}

Data [index] = e

}

/ * *

* whether an element is included in the array

*

* @ param e

* @ return

, /

Public boolean contains (T e) {

For (int I = 0; I

< size; i++) { //注意值的比较应修改为equals方法 if (data[i].equals(e)) { return true; } } return false; } /** * 查询数组中某个元素的索引 * * @param e * @return 没有该元素则返回-1 */ public int find(T e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) { return i; } } return -1; } /** * 删除index位置的元素,并且返回该元素 * 不用担心删除后原来size位置的元素,因为用无法访问到它。 * 但是最好再写一句data[size]=null 具体原因需要了解java的垃圾回收机制 * * @param index * @return */ public T remove(int index) { if (index < 0 || index >

= size) {

Throw new IllegalArgumentException ("Remove failed.Require index > = 0 and index)

< size"); } T ret = data[index]; for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; } //注意维护size size--; data[size]=null; return ret; } /** * 不用担心数组为空的情况,因为数组如果为空,remove方法就会抛出异常 * @return */ public T removeFirst(){ return remove(0); } public T removeLast(){ return remove(size-1); } /** * 从数组中删除元素e * @param e */ public boolean removeElement(T e){ int index=find(e); if(index!=-1){ remove(index); return true; }else{ return false; } } /** * 重写toString()方法 * * @return */ @Override public String toString() { StringBuilder res = new StringBuilder(); res.append(String.format("size= %d,capacity= %d\n", size, data.length)); res.append("["); for (int i = 0; i < size; i++) { res.append(data[i]); if (i != size - 1) { res.append(","); } } res.append("]"); return res.toString(); } } 增加数组大小 前面当我们向数组中添加元素时,如果index==size,表示数组已满。 if (size == data.length) { throw new IllegalArgumentException("Add is fail.Array is full"); } 123 现在可以考虑这样做,依然判断插入位置是否合法,但是当size等于数组长度时,自动为数组扩容--resize(2*data.length); 之所以是扩为原来的2倍,是因为这样扩容量的大小和原来容量大小有关,既不会过小也不会过大。 private void resize(int newCapacity){ T[] newData=(T[])new Object[newCapacity]; for(int i=0;i size) { throw new IllegalArgumentException("Add is fail.Require index >

= 0 and index

< size"); } if(size == data.length){ resize(2*data.length); } for (int i = size; i >

Index; iMurt -) {

Data [I] = data [I-1]

}

Data [index] = e

Size++

}

/ * *

* get the elements of the index location

* through this encapsulation, users cannot query unused space, which ensures the security of the data.

*

* @ param index

* @ return

, /

Public T get (int index) {

If (index

< 0 || index >

= size) {

Throw new IllegalArgumentException ("Get is failed.Index is illegal")

}

Return data [index]

}

/ * *

* modify the elements of the index location

*

* @ param index

* @ param e

, /

Public void set (int index, te) {

If (index

< 0 || index >

= size) {

Throw new IllegalArgumentException ("Set is failed.Index is illegal")

}

Data [index] = e

}

/ * *

* whether an element is included in the array

*

* @ param e

* @ return

, /

Public boolean contains (T e) {

For (int I = 0; I

< size; i++) { //注意值的比较应修改为equals方法 if (data[i].equals(e)) { return true; } } return false; } /** * 查询数组中某个元素的索引 * * @param e * @return 没有该元素则返回-1 */ public int find(T e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) { return i; } } return -1; } /** * 删除index位置的元素,并且返回该元素 * 不用担心删除后原来size位置的元素,因为用无法访问到它。 * 但是最好再写一句data[size]=null 具体原因需要了解java的垃圾回收机制 * * @param index * @return */ public T remove(int index) { if (index < 0 || index >

= size) {

Throw new IllegalArgumentException ("Remove failed.Require index > = 0 and index < size")

}

T ret = data [index]

For (int I = index; I < size-1; iTunes +) {

Data [I] = data [I + 1]

}

/ / pay attention to maintaining size

Size--

Data [size] = null

If (size==data.length/4 & & data.roomthplash 4 times 0) {

Resize (data.length/2)

}

Return ret

}

/ * *

* Don't worry about the empty array, because if the array is empty, the remove method will throw an exception

* @ return

, /

Public T removeFirst () {

Return remove (0)

}

Public T removeLast () {

Return remove (size-1)

}

/ * *

* remove element e from the array

* @ param e

, /

Public boolean removeElement (T e) {

Int index=find (e)

If (indexable colors music 1) {

Remove (index)

Return true

} else {

Return false

}

}

/ * *

* override the toString () method

*

* @ return

, /

@ Override

Public String toString () {

StringBuilder res = new StringBuilder ()

Res.append (String.format ("size=% djimity.com =% d\ n", size, data.length))

Res.append ("[")

For (int I = 0; I < size; iTunes +) {

Res.append (data [I])

If (I! = size-1) {

Res.append (,)

}

}

Res.append ("]")

Return res.toString ()

}

Private void resize (int newCapacity) {

T [] newData= (T []) new Object [newCapacity]

For (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report