ArrayList<Qubit> cycl = new ArrayList<Qubit>();
Qubit num0 = new Qubit(4,5,6,64);
cycl.add(num0);
has error identifier expected. Can you please help?
A few things to try:
Try specifying the type of object to go into the ArrayList: ArrayList<Qubit> cycl = new ArrayList<Qubit>();
Make sure you are importing java.util.ArrayList
Make sure you are calling the constructor correctly
Related
I am converting an app written using Swing to JavaFX. I need an array of TextFields that I can manipulate collectively and as individual array members addressed by the array index. The following code works in Swing but I cannot find the equivalent in JavaFX, using TextField instead of JTextField. How can I achieve this in JavaFX?
private ArrayList<JTextField> fieldList= new ArrayList<JTextField>();
fieldList.add(fldCompletion);
fieldList.add(fldHrsToDate);
fieldList.add(fldHrsToComplete);
for(JTextField fl : fieldList) {
fl.setText("");
fl.setEnabled(true); //FX equivalent is setDisable(false)
}
fieldList.get(var).setText("");
I also don't understand your question because this part of the code should work in JavaFX in the same way as it works in Java(Swing) and you are not telling us what your actual problem is. So I just make a wild guess. Maybe you have just forgotten to add your text field to the scene graph too and therefore your are seeing nothing.
Apologies - it's my first post.
I can instantiate the ArrayList -
private ArrayList<TextField> fieldList = new ArrayList<TextField>();
but when I try to add a TextField object to the array I get syntax errors:
fieldList.add(fldCompletion);
Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", # expected after this token
- Syntax error, insert "SimpleName" to complete QualifiedName
- Syntax error, insert "Identifier (" to complete
MethodHeaderName
This particular field is declared thus:
#FXML private TextField fldCompletion;
This will do just fine
TextField[] txt = new TextField[beanFields.length];
for (int i=0; i<=beanFields.length-1; i++) {
TextField textField = new TextField();
txt[i] = textField;
textField.setPrefWidth(200);
textField.setPrefHeight(32);
}
I have an array named arr of type [Int8]:
var arr: [Int8] = []
Throughout the program I add items to the array using append and insert. However, when I try to remove an item using arr.removeIndexAt(x), it throws the error:
Playground execution failed: <EXPR>:144:13: error: immutable value of type '[Int8]'
only has mutating members named 'removeAtIndex'
arr.removeAtIndex(x)
Why is this happening? I tried recreating this in a playground:
var arr: [Int8] = []
arr.append(1)
arr.removeAtIndex(0)
and it works fine. Could someone please explain to me how I might fix this problem or remove an item another way? Any help wold be great. Thanks :)
Found the solution. Add mutating to your definition of removeExtraZeros() to allow it to alter properties, i.e,
mutating func removeExtraZeros() { ... }
Unfortunately you run into an issue where the while loop after that for loop is looping infinitely, so consider revising that part as well.
You say when you try to remove an item using arr.removeIndexAt(x), it throws the error.
Because the method name is removeAtIndex:, not removeIndexAt:
I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.
A simplified example of the problem.
classdef clsMyClass < handle
properties
dblMyProperty1
end
methods
function obj = clsMyClass()
end
end
end
And when running
vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;
vecMyArray.dblMyProperty1 = 1:100;
We get the following error:
??? Incorrect number of right hand side elements in dot name
assignment. Missing [] around left hand side is a likely cause.
Any help would be appreciated.
I see what you're trying to do now. Use disperse from the MATLAB File Exchange:
>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans =
1
>> vecMyArray(10).dblMyProperty1
ans =
10
You can use the deal function for exactly this purpose:
[vecMyArray.dblMyProperty1] = deal(1:100);
See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html
Edit: No you can't, actually; that'll set them to all be the vector 1:100.
I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.
Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:
So you'll need:
for ii=1:100
vecMyArray(ii).dblMyProperty1 = ii;
end
I know it's not satisfying, but I think it at least helps us to definitively understand this error.
I am fairly new to programming, and I'm trying to do some work with arrays, but I'm getting an error that I don't know how to fix. Any help would be great!
Error: 1084: Syntax error: expecting colon before leftbracket.
Source: hockeyPP({hockeyPlayers[i]});
Error: 1084: Syntax error: expecting identifier before rightbrace.
Source: hockeyPP({hockeyPlayers[i]});
function eliminateAbsentees():void{
for(var i:int=0; i<=hockeyPlayers.length; i++){
if(hockeyPlayers[i].attendance==true){
hockeyPP.push({hockeyPlayers[i]});
}
}
}
remove { and } surrounding hockeyPlayers[i]. Why you want to used it in this way?
function eliminateAbsentees():void{
for(var i:int = 0; i <= hockeyPlayers.length; i++){
if(hockeyPlayers[i].attendance == true){
hockeyPP.push(hockeyPlayers[i]);
}
}
}
As mentioned by Azzy Elvul, your issue was the curly brackets ( "{}" ) around the array item. You'll see curly brackets in a few places:
Function Declarations
Object Declarations
Class Declarations
Loops
Conditionals
I think there is one more, but that is what I came up with off the top of my head. Basically, when you tried to use this line:
hockeyPP.push({hockeyPlayers[i]});
you tried to declare hockeyPlayers[i] as a new Object (the most basic class in ActionScript, and most languages). You can instantiate the Object class by two ways:
var obj:Object = new Object(); and
var obj:Object = {};
You tried to do the second one, the lazy instantiation. So you tried to declare an object with a property of hockeyPlayers[i] without associating a value with it (the basis of all OOP is property:value pairs).
As that first error said, you are missing a colon for that type of instantiation. If you were to try
hockeyPP.push({hockeyPlayers[i] : null}); //null is what an object is when it has no value
you would not have gotten any errors, as that is the correct way to instantiate an Object. For your needs, though, you just want to push an item from one array to another array. So you do
array2.push( array1[ selectedIndex ] );
I would definitely give the LiveDocs some reading. They can seem daunting, but they are incredibly well written and easy to understand once you start going through them.
LiveDocs - Array
LiveDocs - Object
I've imported several images into an actionScript 3 document. I've turned them all into symbols (movie clips) and given them instance names to reference from ActionScript.
Ok, so I'm putting the instances into an array so I can loop through them easily, but for some reason, whenever I'm putting in the instance name, I do a trace on the value in the array and it's giving me the symbol object back, rather than the instance object.
Basically trying to loop through the array to make each instance's visibility = false
Here's a sample:
var large_cap_extrusion_data: Array = new Array();
large_cap_extrusion_data[0] = large_cap_extrusion_menu_button;
large_cap_extrusion_data[1] = extrusion_border_large_cap
large_cap_extrusion_data[2] = "Large Cap";
large_cap_extrusion_data[3] = large_cap_main_menu_button;
var extrusion_data: Array = new Array();
extrusion_data[0] = large_cap_extrusion_data;
trace(extrusion_data[0][0]);
The traces gives:
[object large_cap_menu_button]
(the parent symbol)
rather than:
"large_cap_extrusion_menu_button"
I'd be very grateful if someone could tell me where I'm going wrong...
when you trace and object, by default it describes it type. What you want is the "name" property of the object.
Try this:
trace(extrusion_data[0][0].name);
that should give you the instance nema of the large_cap_menu_button rather than the class description. Either way, you have the right object I bet.