利用反射技术在泛型String集合中存入Integer元素

USB2.0信号完整性测试之眼图 名称线缆颜色引脚描述红色绿色白色GND黑色。 阅读详情

首先我们来了解一下什么叫反射:
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

需求:在不修改源代码的情况下将泛型为 的Arraylist集合中存入Integer元素.
原理: 程序编译后产生的.class文件中是没有泛型约束的,这种现象我们称为泛型的擦除。我们可以通过反射技术,来完成向有泛型约束的集合中,添加任意类型的元素

代码演示如下:

package ArrayAdding;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class ArrayDemo {
	public static void main(String[] args) throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		list.add("array");
		//获得集合对象的Class类
		Class cl =list.getClass();
		//从集合Class类中获取add()方法 参数为object
		Method method = cl.getMethod("add", Object.class);
		//方法唤醒并调用 传入集合对象和需要存储的元素
		method.invoke(list, 123);
		method.invoke(list, 456);
		method.invoke(list, 789);
		System.out.println(list);
	}
}

泛型擦除添加元素后我们打印集合 效果如下:
在这里插入图片描述

擦出泛型后的集合可以使用增强for循环遍历,不过取出元素类型为object;当我们采用迭代器循环遍历时,迭代器泛型任然为String,同样能够取出Integer类型元素,因为在集合内部是一个object数组对象.

MoveIt 运动学、运动规划、混合规划、move_group 混合规划是运动规划方法的一个术语,它将异构运动规划器结合起来,以产生更稳健或反应更快的解决方案。这种通用方法在导航领域已经非常成熟,并在导航2等流行项目中成功实施。MoveIt 的混合规划架构结合了一对全局规划器和局部规划器,它们以不同的规划速度和问题范围并行、并发运行。全局规划器的任务是解决全局运动规划问题,这与 “感知-规划-行动 ”应用中使用的规划器非常相似。所使用的规划器算法应该是完整的,因此假定其计算时间相对较慢。此外,全局规划器不要求实时安全,这意味着无法保证规划器在特定期限内找到解决方案。 阅读详情

相关推荐

R语言笔记三:矩阵、数组、列表和数据框

1.矩阵        矩阵是二维的,需要有行和列。向量是一维的。        在R软件中,矩阵是有维数的向量,这里的矩阵元素可以是数值型、字符型或者逻辑型,但是每个元素必须拥有相同的模式,这个和向量一致。 (1)创建矩阵: m <- matrix(1:20,4,5) 行数和列数的分配必须满足分配条件。若分配的行列过大,分配的元素不足够,则报错。 m <-

weixin_44384631的博客 6211

如何在泛型为Integer的ArrayList中存放String对象

方法:通过反射得到ArrayList的add()方法,用invoke调用这个方法并通过这个方法将字符串添加到集合中去。 上代码: ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= 6; i++) { list.add(i); } String str = "字符串"; Method add = list.g

yuajiang的博客 1085

Webots+UR机械臂仿真全流程:手把手教你用Python实现Pick-and-Place轨迹规划

本文详细介绍了使用Webots和Python实现UR六自由度机械臂的Pick-and-Place轨迹规划全流程。从环境搭建、运动学正逆解计算到轨迹规划实战技巧,涵盖工业场景中的关节补偿、末端校准等实际问题解决方案,帮助开发者快速掌握机械臂精准控制技术。

weixin_29083649的博客 284

Long类型被反序列化为Integer类型问题解决方案

【代码】Long类型被反序列化为Integer类型问题解决方案。

qq_40155680的博客 441

Linux 系统管理员必备:Chrome 企业版离线部署全攻略(含 RPM/DEB 双版本)

本文详细介绍了Linux系统管理员如何离线部署Chrome企业版,涵盖RPM和DEB双版本。从获取官方离线安装包到静默安装、权限配置及企业级策略管理,提供全流程解决方案,特别适合Ubuntu等DEB系系统管理员参考。

f9g0h的博客 493

转录组PCA分析3大常见误区:数据标准化、离群样本与批次效应处理

本文深入探讨转录组PCA分析中的三大常见误区:数据标准化、离群样本识别与批次效应处理。通过实战案例和R代码演示,揭示如何优化PCA结果以提升生物学解释力,特别关注数据标准化策略的选择和批次校正方法的应用,帮助研究者避免常见陷阱并获得可靠的转录组分析结果。

weixin_28362173的博客 374

在泛型为String的集合中利用反射添加一个Intger类型的值

【代码】在泛型为String的集合中利用反射添加一个Intger类型的值。

weixin_72100882的博客 202

STM32H743VIT6最小系统板PCB设计全流程(AD09实战教程+封装库分享)

本文详细介绍了基于STM32H743VIT6高性能MCU的最小系统板PCB设计全流程。内容涵盖从项目规划、原理图设计到PCB布局布线的AD09实战操作,重点解析了高速电路设计中的电源、时钟、去耦等关键模块的处理要点与常见陷阱,并分享了经过验证的封装库资源,旨在为工程师提供一套完整、可靠的设计方法与实战参考。

weixin_29194693的博客 120

JAVA 反射练习

package Reflects; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayLi...

qq_36466946的博客 78

QT5.14.2+VS2019安装配置教程Windows

QT5.14.2+VS2019安装使用教程Windows前言一. QT的下载与安装1. QT的下载2. QT的安装二. 配置Visual Studio编译器环境1. 扩展工具下载2. 扩展工具配置 前言 由于Visual Studio 的功能强大,很多开发人员比较熟悉使用,所以可以通过将QT配置到Visual Studio中进行开发。 一. QT的下载与安装 1. QT的下载 QT5.14.2下载地址 2. QT的安装 断开网络!断开网络!!断开网络!!! 安装QT之前,需要断开电脑的网络连接,这样就

Zander0的博客 7250

从Xilinx FPGA到MIPI DSI LCD屏:手撸代码的实战经验分享

本文分享了使用Xilinx FPGA驱动MIPI DSI LCD屏的实战经验。作者从依赖官方IP核失败,到与底层D-PHY IP缠斗,最终通过手撸代码,直接配置GTY收发器原语并实现完整的D-PHY与DSI协议栈,成功点亮屏幕。文章详细剖析了协议细节、Vivado工具链的调试技巧与避坑指南,为FPGA工程师提供了从理论到实践的宝贵参考。

weixin_29188997的博客 400

java5核心基础之泛型(3)-泛型作用于编译阶段-如何将String对象传入Integer类型的泛型对象中?

泛型作用于编译阶段: 泛型是作用于编译阶段,在编译阶段控制类型,以确保在编写代码的时候只能传入指定类型数据到泛型集合对象中去。如何验证呢,贴代码如下: package highBasic.generic;import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import ja

HYM BLOG 1909

YOLOv11改进策略【SPPF】| AIFI : 基于Transformer的尺度内特征交互,在降低计算成本的同时提高模型的性能

本文记录的是基于AIFI模块的YOLOv11目标检测改进方法研究。AIFI是RT-DETR中高效混合编码器的一部分,利用其改进YOLOv11模型,

Limiiiing的博客 2241

java反射修改泛型约束和String创建方式

java反射的一些小应用

weixin_44884861的博客 687

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

在泛型为Integer的ArrayList中存放一个String类型的对象

通过反射获取list集合的所有方法,然后把字符串存入集合中。

学而时习之 3066

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

string数组转换成int数组,数组转字符串,字符串转list,list集合转字符串,删除数组中为空的元素,list转数组 数组转list

string数组转换成int数组 Array.ConvertAll<string,int>(string的数组,int.Parse); 数组转字符串,以逗号分隔 string[] 数组 = post.体检编号数组; string str = string.Join(",", 数组); list集合转字符串 public static...

你要明白,任何问题都不是孤立存在的,一定有人曾经遇到过,并且已经有更好的解决办法了,只是我还不知道。我不应该在黑暗中独自前行,去重新发明轮子,也许我的顿悟,只是别人的基本功!我应该要站在巨人的肩膀上,学习更成熟的经验和方法,然后再来解决这个问题 2000

一个int类型的数字在不转型的前提下怎么放入List<String>?

结论:由此可见,通过翻译绕过编译期检查,理论上Object类型都能塞入list

liminghui4321的博客 324

创建一个ArrayList<String> 集合,通过反射向集合中添加Integer类型的数据

创建一个ArrayList<String> 集合,通过反射向集合中添加Integer类型的数据 反射可以越过泛型的机制。

小羊的博客 1091

JAVA通过反射实现向ArrayList<Integer>集合中添加字符串数据

反射实现向ArrayList集合中添加字符串数据 public class test { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { ArrayList<Integer> array=new ArrayList<Integer>(); array.a

jb18602754485的博客 1229

绕过泛型,通过反射把 String 添加到 List<Integer> 中

绕过泛型,通过反射把 String 添加到 List&lt;Integer&gt; 中 public class ArrayListDemo { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {...

qq_41040268的博客 444

利用反射机制在泛型为Integer的ArrayList集合中添加5个元素,删除脚标为3的元素。

利用反射机制在泛型为Integer的ArrayList集合中添加5个元素,删除脚标为3的元素。 public static void main(String[] args) throws Exception{ ArrayList<Integer> arrayList=new ArrayList<>(); //获取类对象 Cla...

nore的博客 1397

java面试要点---ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。...

java面试要点---ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。 ArrayListlist=newArrayList();在这个泛型为Integer的ArrayList中存放一个String类型的对象。 1.刚刚看到的时候,也是很纳闷后来仔细看了下,java的反射机制;2.这...

管子(zero)的杂乱空间 1628

java 整数字符串当整数存入list_java – 如何将一个整数添加到一个String ArrayList?...

您使用了类型擦除,这意味着您已经忽略了以前设置的通用检查.你可以把它作为这个,因为泛型是一个编译时功能,在运行时没有检查.你有什么相同的List list = new ArrayList() ;list.add(1) ;Integer hello = (Integer) list.get(0) ;System.out.println(hello);要么List list = new ArrayLi...

weixin_32567865的博客 475

使用反射在泛型为Integer的ArrayList中添加String类型的数据

昨天刚刚回顾了一下曾经了解过但是还是不懂的反射,看到了一个有意思的代码,如何在泛型为Integer的ArrayList中添加一个String类型的数据呢?private void addStringToIntList() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

yanxiaosa的博客 4536

List<String>存放Integer类型数据——通过反射越过泛型检查

如需转载分享,请标明出处,且不用于盈利为目的,谢谢合作! List<String>存放Integer类型数据——通过反射越过泛型检查 import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.u...

এ某年某月某日এ的博客 518

JAVA基础之String与Integer的转换

String与Integer的相互转化

小黑的博客 2万+

Java面试题-反射案例

需求说明 现有集合:ArrayList list = new ArrayList(); 利用反射机制在这个泛型为Integer的ArrayList中存放一个String类型的对象。 public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAcce...

奋斗吧010 1170
上一篇: 自主代码封装LinkedList集合对象
下一篇: 用面向对象思想实现ArrayList代码
Java小柴
博客等级 码龄8年 46粉丝 24原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值