如何在java中初始化动态数组?

编程入门 行业动态 更新时间:2024-10-27 16:26:20
本文介绍了如何在java中初始化动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有一个类需要返回一个变量维的字符串数组(并且该维度只能在运行该类的某个方法时确定),那么如何在类的构造函数中声明动态数组呢? / p>

如果问题不够明确,

在php中我们可以简单地将字符串数组声明为 $ my_string_array = array(); 并通过 $ my_string_array [] =新值添加元素;

上面的代码是什么,然后在java中?

解决方案

您将需要查看 java.util包,特别是 ArrayList 类。它有一些方法,如 .add()。remove()。indexof()。contains()。toArray()等等。

If I have a class that needs to return an array of strings of variable dimension (and that dimension could only be determined upon running some method of the class), how do I declare the dynamic array in my class' constructor?

If the question wasn't clear enough,

in php we could simply declare an array of strings as $my_string_array = array(); and add elements to it by $my_string_array[] = "New value";

What is the above code equivalent then in java?

解决方案

You will want to look into the java.util package, specifically the ArrayList class. It has methods such as .add() .remove() .indexof() .contains() .toArray(), and more.

更多推荐

如何在java中初始化动态数组?

本文发布于:2023-11-22 23:45:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   初始化   动态   如何在   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!