溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在java中對數組進行擴容

發布時間:2020-12-28 14:48:43 來源:億速云 閱讀:226 作者:Leah 欄目:開發技術

這篇文章將為大家詳細講解有關怎么在java中對數組進行擴容,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

Java數組擴容的原理

1)Java數組對象的大小是固定不變的,數組對象是不可擴容的。

2)利用數組復制方法可以變通的實現數組擴容。

3)System.arraycopy()可以復制數組。

4)Arrays.copyOf()可以簡便的創建數組副本。

5)創建數組副本的同時將數組長度增加就變通的實現了數組的擴容。

數組擴容的三種方式:

新建一個數組,把原來數組的內容搬到 新數組中。

用系統定義函數system.arraycopy實現擴容;

用系統定義函數copyof函數實現擴容;

下面用程序來實現這三種擴容

class expand2{
 //利用函數的方法進行數組的擴充
 public static void main(String[] args) {
 //定義一個小型的數組
 int[] a={1,2,3,5};
 //調用擴容函數
 //a=expand2(a);
 //a=expand3(a);
 a=expand4(a);
 //測試是否擴容完成,輸出此時數組a中的值
 for (int i=0;i<a.length;i++) {
 System.out.println("aaaa:"+a[i]);
 }
 }

 //擴容函數,
 public static int[] expand2(int a[]){
 //定義一個新數組b,并為其賦值長度為數組a的二倍
 int b[] = new int[a.length*2];
 //將數組a的元素循環遍歷到數組b中
 for (int i=0;i<a.length;i++) {
 b[i] = a[i];
 }
 //返回擴容后的數組b
 return b;
 }

 //數組擴容方法3,利用系統函數arraycopy進行擴容
 public static int[] expand3(int a[]){
 int[] b = new int[a.length*2];
 //系統函數進行擴容,將a[]的值賦值到b[]中,共a.length個長度。
 //相當于第19-21行
 System.arraycopy(a,0,b,0,a.length);
 return b;
 }
 
 //數組擴容方法4,利用系統函數copy進行擴容
 public static int[] expand4(int a[]){
 //可以查看api文檔,java.util.Arrays.copyOf的詳細使用;
 return java.util.Arrays.copyOf(a,a.length*2);
 }
}

實現案例:

案例1 : 統計一個字符在字符串中的所有位置.

字符串: 統計一個字符在字符串中的所有位置

字符: '字'

返回: {4,7}

public class CountCharDemo {
 public static void main(String[] args) {
  char key = '字';
  String str = "統計一個字符在字符串中的所有位置";
  int[] count=count(str,key);
  System.out.println(Arrays.toString(count));//[4, 7]
 }
 public static int[] count(String str,char key){
  int[] count={};
  for(int i=0;i<str.length();i++){
   char c=str.charAt(i);
   if(c==key){
    //擴展數組
    count=Arrays.copyOf(count, count.length+1);
    //添加序號i
    count[count.length-1]=i;
   }
  }
  return count;
 }
}

char[]、String、StringBuilder

char[]:字符序列, 只有字符數據, 沒有操作, 如果算法優秀, 性能最好。

String: char[] + 方法(操作, API功能)

StringBuilder: char[] + 方法(操作char[] 的內容)

String:內部包含內容不可變的char[],表現為String對象不可變。String包含操作(API方法),是對char[]操作,但不改變原對象經常返回新的對象,很多String API提供了復雜的性能優化算法,如:靜態字符串池。

StringBuilder:內部也是一個char[],但是這個數組內容是可變的,并且自動維護擴容算法,因為數據內容可變,所以叫:可變字符串。StringBuilder API方法,是動態維護char[]內容,都可以改變char[]內容。

public abstract class AbstractStringBuilder {
 /** The value is used for character storage.*/
 char value[];
 /** The count is the number of characters used.*/
 int count;
 /** Returns the length (character count).*/
 public int length() {
  return count;
 }

 public AbstractStringBuilder append(String str) {
  if (str == null)
   str = "null";
  int len = str.length();
  if (len == 0)
   return this;
  int newCount = count + len;
  if (newCount > value.length)
   expandCapacity(newCount);
  str.getChars(0, len, value, count);
  count = newCount;
  return this;
 }

 /**
  * 自動實現Java數組擴容
  */
 void expandCapacity(int minimumCapacity) {
  int newCapacity = (value.length + 1) * 2;
  if (newCapacity < 0) {
   newCapacity = Integer.MAX_VALUE;
  } else if (minimumCapacity > newCapacity) {
   newCapacity = minimumCapacity;
  }
  value = Arrays.copyOf(value, newCapacity);
 }
}

字符串數組與String類的原理

/** 字符串數組與String類的原理 */
public class CharArrayDemo {
 public static void main(String[] args) {
  /* Java 可以將char[]作為字符串處理 */
  char[] ch2={'中','國','北','京'};
  char[] ch3={'歡','迎','您'};
  System.out.println(ch2);//中國北京
  System.out.println(ch3);//歡迎您
  /* char[]運算需要編程處理,如連接: */
  char[] ch4=Arrays.copyOf(ch2, ch2.length+ch3.length);
  System.arraycopy(ch3, 0, ch4, ch2.length, ch3.length);
  System.out.println(ch4);//中國北京歡迎您
  /* String API提供了簡潔的連接運算: */
  String str1="中國北京";
  String str2="歡迎您";
  String str3=str1.concat(str2);
  System.out.println(str3);//中國北京歡迎您
  /* 字符串轉大寫: */
  char[] ch5={'A','a','c','f'};
  char[] ch6=Arrays.copyOf(ch5, ch5.length);
  for(int i=0;i<ch6.length;i++){
   char c=ch6[i];
   if(c>='a' && c<='z'){
    ch6[i]=(char)(c+('A'-'a'));
   }
  }
  System.out.println(ch6);//AACF, 原數組ch5不變
  String str4="Aacf";
  String str5=str4.toUpperCase();//原字符串str4保持不變
  System.out.println(str5);//AACF
 }
}

關于怎么在java中對數組進行擴容就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女