How to pass a list element as function parameter? - arrays

I have a char array: my_array={"a","b","c"}
And I need to get 1 specific element and put it as function parameter
For example: function myFunction({"b"}){}
I tried this but got an error:
unexpected character
I learned from TTCN-3 and I try this on it.

This will help you. Use this kind of code will help you to pass one parameter each time.
public class MyClass {
public static void main(String args[]) {
MyClass mc = new MyClass();
char my_array[]={'a','b','c'};
char returnValue;
for(int i=0;i<my_array.length;i++){
returnValue = mc.charReturn(my_array[i]);
System.out.println("Got it from method:"+returnValue);
}
}
public char charReturn(char fromClass){
return fromClass;
}
}

Related

ArrayList and initializing a 2D array not working?

I'm learning the AP CSA course and tried to print these arrays. It keeps saying "can't find symbol" and not gonna lie I have absolutely no idea what that means.
//ArrayList:
public class al{
public static void main(String[] args){
List l = new ArrayList();
System.out.print(l.size());
}
}
//2DArray:
public class arrays{
public static void main(String[] args){
int[][] arry = {{1,2,3},
{1,2,3},
{1,2,3}};
printArray(arry);
}
}
In your first program to print the ArrayList, it is failing because you need to import the List and ArrayList in your program, please try this
import java.util.ArrayList;
import java.util.List;
public class al{
public static void main(String[] args){
List l = new ArrayList();
System.out.print(l.size());
}
}
In the second program the printArray method you called is missing in definition, i.e you have to define the printArray method in your class, you are using it from method main then you need to declare it static like below
static void printArray(int[][] arr){
for(int i=0; i<arr.length; i++){
String row = "";
for(int j=0; j<arr[0].length;j++){
row+=arr[i][j]+" ";
}
System.out.println(row);
}
}
Also while you are learning, please also read about the naming conventions in java (i suppose you are learning java), like a Class name should start from a capital letter.

Unity JSON creation showing error on array

When I am trying to create a JSON using the below code it is showing a null exception error here.
myObject.test[0]="0";
myObject.test[1]="1";
myObject.test[2]="2";
How to fix this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JSON_Manager : MonoBehaviour
{
public string JSON;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Node myObject = new Node();
myObject.BrickName = "test";
myObject.LED=false;
myObject.test[0]="0"; //error
myObject.test[1]="1";
myObject.test[2]="2";
string JSON = JsonUtility.ToJson(myObject);
print(JSON);
}
}
[System.Serializable]
public class Node
{
public string BrickName;
public bool LED;
public string[] test;
}
This error happens since you are not initializing array. The initial capacity is 0, that is why it gives an error.
The easiest way to fix it is to write like that before accessing to the string or maybe create default constructor that will do it for you:
test = new string[size you need]

Trouble reading from Storage

Ive had a deeper look at the storage system now, but I just can't get it to work properly. The data seems to be saved, the saved data is definitely not empty and I am still getting a java.lang.NullPointerException.
So here is my Class i want to saved and later read to the Storage.
Band is a trivial Class with name and genre String.
public class Band implements Externalizable{
String name;
String genre;
public Band(String bnd, String gen){
this.name = bnd;
this.genre = gen;
}
public Band() {
}
public String getName(){
return this.name;
}
public String getGenre() { return this.genre; }
public void setName(String name) {
this.name = name;
}
public void setGenre(String genre) {
this.genre = genre;
}
#Override
public String toString(){
String s;
s = this.name;
return s;
}
public int getVersion(){
return 1;
}
#Override
public void externalize(DataOutputStream out) throws IOException {
Util.writeUTF(name, out);
Util.writeUTF(genre, out);
}
#Override
public void internalize(int version, DataInputStream in) throws IOException {
name = Util.readUTF(in);
genre = Util.readUTF(in);
}
public String getObjectId(){
return "Band";
}
}
BandList is just a Class with an ArrayList the Bands are added to.
public class BandList implements Externalizable{
List <Band> bandList;
public List getBandList(){
return this.bandList;
}
public BandList(){
bandList = new ArrayList<Band>();
}
public void setBandList(List<Band> bandList) {
this.bandList = bandList;
}
public int getVersion(){
return 1;
}
#Override
public void externalize(DataOutputStream out) throws IOException {
Util.writeObject(bandList, out);
}
#Override
public void internalize(int version, DataInputStream in) throws IOException {
bandList = (ArrayList<Band>) Util.readObject(in);
}
public String getObjectId(){
return "BandList";
}
}
So, in some other class where the user filled in some TextFields, the band gets added to the BandList and therefore I call saveListsToStorage();
public void saveListsToStorage(){
Storage.getInstance().clearStorage();
Storage.getInstance().writeObject("Bands", bandList);
}
So now, when starting the App, the App is looking for Data in the Storage, if found, it will return the BandList Object stored, otherwise it will return a new BandList Object.
Before that, I regist the Classes with
Util.register("Band", Band.class);
Util.register("BandList", BandList.class);
and finally call this method in the main at the beginning:
public BandList loadSavedBandList() {
String[] temp = Storage.getInstance().listEntries();
for (String s : temp) {
if (s.equals("Bands") == true) {
BandList tempBand = (BandList) Storage.getInstance().readObject("Bands");
return tempBand;
}
}
return new BandList();
}
After saving and then loading it throws the NullPointer exception and I have no clue why. I now used ArrayList instead of LinkedList as Shai told me and I implemented the Externalizable Interface as told in the guide.
Maybe some of you can tell me whats wrong here.
Edit:
Here is the Exception:
java.lang.NullPointerException
at com.rosscode.gehma.Menu_Form.updateBandContainer(Menu_Form.java:95)
at com.rosscode.gehma.Menu_Form.<init>(Menu_Form.java:73)
at com.rosscode.gehma.main.start(main.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.codename1.impl.javase.Executor$1$1.run(Executor.java:106)
at com.codename1.ui.Display.processSerialCalls(Display.java:1152)
at com.codename1.ui.Display.mainEDTLoop(Display.java:969)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Edit 2, Line 95:
if(bandList.getBandList().isEmpty() == false) {
for (int i = 0; i < bandList.getBandList().size(); i++) {
Button temp = new Button(bandList.getBandList().get(i).toString());
temp.addActionListener((e) ->
nextStep()
);
listCont.add(temp);
}
menuForm.revalidate();
}
NullPointerExceptions are pretty amazing things, because not only are they easy to figure out, they even show you the exact line where they happen. All you have to do is look at anything left of a . and check if it's null. As soon as you find it, you find the problem.
So let's look at your code. Sadly I don't know which of those lines is line 95 but I'm guessing it's the one starting with if.
That means either bandList is null or bandList.getBandList() returns null. A quick look into getBandList() shows that it won't be null, as you initialize the list in the constructor of your BandList class. Which only leaves bandList.
Sadly you didn't post where you got that from, but I'm gonna assume you just did something like:
BandList bandList = loadSavedBandList();
So let's look into that method. (btw: I say a bit more about where you have to look if you didn't do this at the end of this answer).
If you couldn't find your key, it would return a new BandList, which couldn't be null. So it has to find something, meaning the key exist but the value is null.
So let's take another step and look at how you save things in the first place.
public void saveListsToStorage(){
Storage.getInstance().clearStorage();
Storage.getInstance().writeObject("Bands", bandList);
}
Now "in some other class" doesn't really explain much. But since you read a null, it means you are writing a null here. Which would mean in this "other class", at least in this method, bandList = null.
With the code you gave, it's impossible for me to look deeper into this issue, but it seems like in "that other class", you got a class attribute named bandList but fail to put anything inside it.
Or maybe you saved everything right, but didn't call
BandList bandList = loadSavedBandList();
in some form or another before your if, which would also pretty much explain the problem. I mean... if you never load the bandList, it will obviously be null...

How to define an array in hadoop partitioner

I am new in hadoop and mapreduce programming and don't know what should i do. I want to define an array of int in hadoop partitioner. i want to feel in this array in main function and use its content in partitioner. I have tried to use IntWritable and array of it but none of them didn't work . I tried to use IntArrayWritable but again it didn't work. I will be pleased if some one help me. Thank you so much
public static IntWritable h = new IntWritable[1];
public static void main(String[] args) throws Exception {
h[0] = new IntWritable(1);
}
public static class CaderPartitioner extends Partitioner <Text,IntWritable> {
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
if you have limited number of values, you can do in the below way.
set the values on the configuration object like below in main method.
Configuration conf = new Configuration();
conf.setInt("key1", value1);
conf.setInt("key2", value2);
Then implement the Configurable interface for your Partitioner class and get the configuration object, then key/values from it inside your Partitioner
public class testPartitioner extends Partitioner<Text, IntWritable> implements Configurable{
Configuration config = null;
#Override
public int getPartition(Text arg0, IntWritable arg1, int arg2) {
//get your values based on the keys in the partitioner
int value = getConf().getInt("key");
//do stuff on value
return 0;
}
#Override
public Configuration getConf() {
// TODO Auto-generated method stub
return this.config;
}
#Override
public void setConf(Configuration configuration) {
this.config = configuration;
}
}
supporting link
https://cornercases.wordpress.com/2011/05/06/an-example-configurable-partitioner/
note if you have huge number of values in a file then better to find a way to get cache files from job object in Partitioner
Here's a refactored version of the partitioner. The main changes are:
Removed the main() which isnt needed, initialization should be done in the constructor
Removed static from the class and member variables
public class CaderPartitioner extends Partitioner<Text,IntWritable> {
private IntWritable[] h;
public CaderPartitioner() {
h = new IntWritable[1];
h[0] = new IntWritable(1);
}
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
Notes:
h doesn't need to be a Writable, unless you have additional logic not included in the question.
It isn't clear what the h[] is for, are you going to configure it? In which case the partitioner will probably need to implement Configurable so you can use a Configurable object to set the array up in some way.

Can't get a CCPointArray to work in Cocos2D-x

I want to create a an array of points (_grid). However, I can't seem to use this CCPointArray anywhere except the function it's created in. I've tried making it public in my class and declaring it in my header, but all fail. Any tips?
after
CCPointArray* p = CCPointArray::create(8);
did you call
p->retain();
?
and remember to release it in your destructor or onExit();
in your YOUR_CLASS.h file
class YOUR_CLASS : public cocos2d::CCLayer {
CCPointArray* p;
public:
CREATE_FUNC(YOUR_CLASS);
bool init();
void onExit();
}
in your YOUR_CLASS.cpp file
bool YOUR_CLASS::init(){
if(CCLayer::init()){
p = CCPointArray::create(8);
p->retain();
return true;
}
return false;
}
void YOUR_CLASS::onExit(){
CCLayer::onExit();
p->release();
p = NULL;
}

Resources