ArrayList - Im getting error 'E cannot be resolved to a type' - arraylist

While trying ArrayList, I'm getting an error 'E cannot be resolved to a type', I want to use different data types in a program, instead of declaring particular data type, I'm using 'E', which I guess accepts all type of data types. Please check the below code, I'm using Java 1.8 version.
public class ArrayListDemo1 {
public static void main(String[] args) {
ArrayList<E> ar1=new ArrayList<E>(); //'E' displaying error(on mouse hovering its stating that 'E cannot be resolved to a type')
ar1.add("Abc");
ar1.add(123);
ar1.add("Xyz");
}

Related

How to write a List of a specific type using kryo serializer

I am trying to write a List of a specific type using Kryo serializer but I am getting errors when I try to read/write it. I am using source provided by apache spark for KryoRegistrator
List I am trying to write is of type List<A> which could be an ArrayList or any other type of list
Code
Class FakeRegsitrator implements KryoRegistrator{
#Override
public void registerClasses(Kryo kryo) {
CollectionSerializer listSerializer = new CollectionSerializer();
listSerializer.setElementClass(A.class, kryo.getSerializer(A.class));
listSerializer.setElementsCanBeNull(false);
kryo.register(A.class, new Serializer<A>(true, true) {
public void write(Kryo kryo, Output output, A a) {
output.writeLong(a.getFieldA)
output.WriteString(a.getFieldB)
}
public A read(Kryo kryo, Input input, Class type) {
return new A(input.readLong(), input.readString())
}
}
}
What am I missing here?
I was able to get it working by registering Arraylist.class
code :
kryo.register(ArrayList.class);
in read method use
kryo.readObject(input, ArrayList.class);
in write method use
kryo.writeObject(output, entry.getArchivePortions());

How do you create a complex variable in java?

I'm using eclipse and messing around with complex numbers. I am not sure how to do this. I know how to do an integer and a double but not the complex type. I found something that said to do
Complex c= new Complex(1.0.3.0); // 1+3i
but that doesnt quite work out. It just gives an error that says the constructor is undefined?
Do you have a Complex.class file in the same folder as your other code?
I grabbed a Complex class from here, created the Complex.class file with javac Complex.java and then running the following code (filenames example.java and example.class) was not a problem:
class example {
public static void main(String [] args) {
Complex a = new Complex(1,2);
}
}

WCF deserialization of type object Properties

I am having trouble achieving the following scenario.
We currently have a method which expects a list of 'context' key value pairs. The value however can be of any type.
the goal is to make this method available using WCF. So I created a
public List<Element> Transform(List<Element> elements)
{
... Transformation of elements takes place
}
[DataContract(Namespace = Constants.NAMESPACE)]
public struct Element
{
[DataMember(Order = 0, IsRequired = true)]
public string Key;
[DataMember(Order = 1, IsRequired = true)]
public object Value;
}
When I use a .Net test project everything works fine.
However, when I call this service using SOAP UI I get an error message:
The formatter threw an exception while trying to deserialize the
message: There was an error while trying to deserialize parameter
elements. The InnerException message was 'Element Value cannot have
child contents to be deserialized as an object. Please use XmlNode[]
to deserialize this pattern of XML.'.
I am having trouble figuring out what to do. any help appreciated.
the xml i use is this:
<ws:Transform>
<ws:elements>
<ws:Element>
<ws:Key>Key1</ws:Key>
<ws:Value>A</ws:Value>
</ws:Element>
<ws:Element>
<ws:Key>Key2</ws:Key>
<ws:Value>B</ws:Value>
</ws:Element>
<ws:ScriptName>SetVariable</ws:ScriptName>
</ws:elements>
</ws:Transform>
In this case, SoapUI uses .Net technology which does not understand target type for object.
sending object is not valid across all platforms. In fact you might get an error with a .Net client as well. Your best bet is create a generic xml representation of the Value and have all clients inflate the object from the xml

Passing class object as argument in Windows 8 C++ App

I want to pass a simple class object from one XAML page to another in a C++ Windows 8 application. I have created a class in App.xaml.h:
ref class StaticInfo sealed {
public:
property Platform::String^ sName;
};
I haved added this to MainPage.xaml to navigate to Page2.xaml:
void StaticApp::MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
StaticInfo^ StaticData;
StaticData->sName=ClickText->Text;
Frame->Navigate(Page2::typeid,StaticData);
}
And then received the smart pointer in Page2:
void Page2::OnNavigatedTo(NavigationEventArgs^ e)
{
(void) e; // Unused parameter
StaticInfo^ data = (StaticInfo^)e->Parameter;
}
I get this error when I navigate to Page2:
Unhandled exception at 0x003A5EE1 in StaticApp.exe: 0xC0000005: Access violation reading location 0x00000010.
Can anyone tell me the exact way to do it. If possible please provide a link that teaches passing data between pages. What if I want to pass multiple objects of different classes?
You need to actually create the StaticInfo object before you start using it:
StaticInfo^ StaticData = ref new StaticInfo();
StaticData->sName=ClickText->Text;

struts2: select tag doesn't like beans with "parameters" property?

I have a base class ReportElement which has type property:
public abstract class ReportElement {
private ReportElementType type;
public ReportElementType getType() {
return type;
}
public void setType(ReportElementType type) {
this.type = type;
}
}
ReportElementType is just an enum with specified code and i18nKey properties for each element. I have a couple of subclasses of ReportElement, each of them introducing their own properties. One of them is Plot:
public class Plot extends ReportElement {
public Plot() {
setType(ReportElementType.PLOT);
}
private Collection<Parameter> parameters = new ArrayList<Parameter>();
public Collection<Parameter> getParameters() {
return parameters;
}
}
On some page I needed to display a collection of different ReportElement instances, so I just used struts2 select tag:
<s:select list="myElements" listKey="type.code" listValue="type.i18nKey" size="20"/>
This worked like a charm for every element except for Plot instaces. Instead of invoking getType().getCode() or getType().getI18nKey() plain toString() was invoked on every instance of Plot! After several hours of fun debugging I noticed that during tag evaluation Plot's getParameters() method is called! So it seems struts was trying to evaluate type.code and type.i18nKey using getParameters() method! Failing to do that it ignored the existence of the properties, that I have clearly specified for usage!
After renaming getParameters to a kind of odd name like getParamms the problem gone. Also the problem hasn't occured when using iterator tag together with property tag instead of select tag.
Does anyone have an idea WHY struts select tag uses parameters property of my bean, when I have clearly specified what property should be used? Is it some "cool" feature or a bug?
P.S. I use struts 2.2.3.1
The argument used in all the FreeMarker templates representing a tag's parameters is called parameters. By providing a parameters property that takes precedence, S2 was unable to get to the object on the stack containing the tag's parameters.
It's neither a cool feature nor a bug, it's just how the templates are implemented. Checking the template source may have saved the few hours of debugging.
Found corresponding issue in struts JIRA: https://issues.apache.org/jira/browse/WW-3268
2.3 is specified as fix version.