Multi-dimension Array Declaration And Instantiation?

May 17, 2015

Whilst pre-preparing for java certification, one of the online mock exams has slightly confused me by saying my answer was incorrect for multi-dimension array 'declaration and instantiation'.

This is one of the answers i chose - which was marked as incorrect

a)
int[][] array2d = {{123}, {4,5}};

Which looks absolutely fine to me.One of the other answers, which i agree is correct and so does the mock exam is

b)
int [][] array2d = new int[2][2];

View Replies


ADVERTISEMENT

Java Length Of One Dimension From A Multidimensional Array?

Apr 8, 2014

how you can do array.length, I have an array with [][][][], and what do I do to find the length of one section?

So let's say

Java Code: int array[][] = {
{5, 5, 5, 5},
{4, 4, 4, 4},
{3, 3, 3, 3}
} mh_sh_highlight_all('java');

How do I find out the length of the {5, 5, 5, 5} which is 4. Because there are 4 place-holders.Is there a way?

View Replies View Related

Array Reference Instantiation

May 17, 2014

I am currently learning Java basic concepts and this is a very simple code which produces error depending on the way I write. It is as follows:

package testPackage;
interface ArrayInsideClassShock {
}
class Check{
ArrayInsideClassShock var[]; //Error line
var= new ArrayInsideClassShock[10];
}

Error in eclipse:Syntax error on token ";", , expected

But if I write it as:

package testPackage;
interface ArrayInsideClassShock {
}
class Check{
ArrayInsideClassShock var[]=new ArrayInsideClassShock[10];
}

It doesn't show any error.

View Replies View Related

Two String Array Declaration?

Apr 30, 2015

My Question is Suppose I am declaring an String array as

String[] a
or
String a[]
or
Comparable[] a

Will there be any performance or other advantage or disadvantage using these syntax?

View Replies View Related

How To Initialize Multidimensional Array Just After Declaration

Feb 13, 2014

I tried this:

String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};

I also tried this:

String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};

but I get an error

View Replies View Related

Multi-Array Of JButtons

May 18, 2014

Java Code:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Screen implements ActionListener {
public JButton[][] b=new JButton[200][200];

[Code] ....

I am trying to create the A* Algorithm and I REALLY need a 2D array to handle this.

This is the error:

Java Code:

at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) mh_sh_highlight_all('java');

View Replies View Related

Multi Array Of Buttons?

May 18, 2014

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[Code]....

I am trying to program an A* algorithm and I cannot even get past the buttons. I need a 2D array of buttons to handle this problem but I am getting the following errors.

at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)

[Code] ....

View Replies View Related

How To Access Element Of Multi Dimensional Int Array Using For Loop

Jul 12, 2014

I have written following code.

I want to print all elements in Array to output here is my code

int[][] arr={{12,12,13},
{14,1223,14}};
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
System.out.println(arr[i][j]);
}
}

but I am getting following output

12
12
14
1223

how to print all elements of int array?

View Replies View Related

Create Mathematical Vector Of D Dimension Initialized At 0

Nov 14, 2014

class GVector {
// TODO: declare a private array to save the vector coordinates 
// Creates a mathematical vector of d dimensions, initialized at 0
public GVector(int d) {
// TODO: implementation

[Code] ....

I'm confused with what type of array I need to use to save the vector coordinates and what to put in Gvector. Is it a constructor?

View Replies View Related

Multi-sided Dice - Keep Track Of Total Sum Of All Dice In Array

May 7, 2014

I'm finishing up this assignment, and I'm stuck. These are the last 2 instructions:

. Roll each of the Dice by invoking the roll method on each Dice in the array.
. Keep track of the totals sum of all the dice in the array. Be sure to roll the dice array at least 10000 times.

How to finish it up?

Here's my code so far:

package homework3;

import java.util.Random;
public class Dice {
private int numberShowing;
private final int numberOfSides;
private static final Random randomNumber = new Random();
public Dice() {
numberOfSides = 6;

[Code] ....

View Replies View Related

Generic Instantiation Of A Collection - ArrayList

Nov 18, 2014

So I have a persons class:

public class Person
{
private String name;
private int age;
 public Person (String name, int age) {
this.name = name;
this.age = age;

[Code] .....

And I need to write a simple main method that creates lots of instances of the Person class and adds them to a generic instantiation of a Collection (ArrayList). And I need to make it so I as a programmer can define how many instances to create.

View Replies View Related

Instantiation Efficiency - Declare Variables To Be Used In A Loop

Sep 6, 2014

Where usually to instantiate and declare variables to be used in a loop. If you declare it outside to be used in the loop it will still be there when the loop is done, never to be used again and is just sitting there taking up memory. However, if you declare it inside the loop, you have the issue of it constantly creating space for said variable, but once it's out of scope it's gone. Is there any advantage performance wise to doing it either way?

View Replies View Related

Swing/AWT/SWT :: Instantiation Of Listener Using Anonymous Inner Class

Jun 2, 2014

JMenuItem blueChoice = new JMenuItem("Blue");
blueChoice.addActionListener(new InnerMenuListener(){
public void actionPerformed(ActionEvent e){
String buttonCommandString = e.getActionCommand( );
if (buttonCommandString.equals("Red"))
redPanel.setBackground(Color.RED);
else if (buttonCommandString.equals("White"))
whitePanel.setBackground(Color.WHITE);
else if (buttonCommandString.equals("Blue"))
bluePanel.setBackground(Color.BLUE);
else
System.out.println("Unexpected error.");
}
});

Can a method in an anonymous inner class ever be private? why or why not?

View Replies View Related

Correct Declaration Of Method

Oct 26, 2014

Netbeans tells me it's an illegal start of expression during the initialisation of the interactWithUser method.
public class InvertLetter {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/**
* String mit den Kleinbuchstaben.
*/
final String lowercase = "abcdefghijklmnopqrstuvwxyz";

[Code] ....

View Replies View Related

Variables Declaration And Visibility

Apr 9, 2015

Let's say within a class I create a method that takes care of creating a java swing layout with labels, buttons etc.. then attach an action listener (inner class) for each button to change a respective label text. All I would need is that the action listener method can access and modify the label as needed.

Have read about static, protected, private, getters and setters but honestly bit confused about which structure should be adopted as a best practice. Global static protected variables for the labels along with private inner classes implementing ActionListeners believe will do the trick and will be able to access the labels but not convinced this is good practice.

View Replies View Related

Invalid Method Declaration

Feb 26, 2015

import java.util.HashSet;
import java.util.ArrayList;
public class Graph
{
double [] [] adj;
graph (double [] [] a)
{
adj= new double [a.length][a.length];
for (int i=0;i<a.length;i++)
for (int j=0;j<a.length;j++)
adj[i][j]=a[i][j];
}

C:UserscDesktopGraph.java:: error: invalid method declaration; return type required
graph (double [] [] a)

View Replies View Related

Objects Inside Class Declaration

Apr 13, 2014

Ok say you have

public class MyClass {
private int x = 5;
Object myObject = new Object();
public MyClass(){
}
}

When would the myObject be created? Before or after the constructor? And does this mean you can't pass
this into the parameters of that object?

View Replies View Related

Final Declaration Statement - How To Use Named Constant

Feb 27, 2014

I'm new to Java and have been stuck on how to use a final declaration statement once it's made. Below is a class I'm creating with the intention of calling it under a main method. I don't understand if I'm supposed to do anything else, like do some sort of get/set, or if the final static line is all I need. And, I don't know how I call it to the main method once I do.

public class Shirt//class name.
{
int collarSize;//data field.
int sleeveLength;//data field.
public final static String MATERIAL = "cotton";//final data field for material.

[Code] ....

View Replies View Related

JSP :: Unnecessary Complications In JSTL Taglib Declaration

Dec 23, 2014

I started learning JSTL (Java Standard Tag Library) and i got to know that i need to mention this directive in JSP page

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

in order to use JSTL tags.

I am not able to understand what does this URI exists at all ?? I feel it making things complex and meaningless or else i am missing something hidden and secret.

View Replies View Related

Java Object Declaration Beyond Common Idiom

Feb 5, 2015

when a new object is created in Java it follows the idiom:

Object obj = new Object();
where the Object() constructor matches the object type Object.

But what if it doesn't? I understand from the Oracle Docs on creating objects and polymorphism that the constructor must be in that object's class or one of its subclasses. However, suppose we wanted to declare a new stack. My first instinct would be:

Stack s1 = new Stack();
But I assume it's valid to do it this way, too:

Object s2 = new Stack(); // Is there a difference here? What are we really saying about s2? I'm guessing s2 is simply an empty stack, but only has access to the Object class methods? I'm not sure why someone would ever do this, but I want to solidify my understanding of the Java hierarchy. Are there really any circumstances where someone would use a subclass's constructor when creating a new object?

View Replies View Related

Method Declaration - Partial Interface Implementation

Sep 3, 2014

I have one interface with three(more than one) method declaration. In the subclass that implements it I want to define only one method not all three not even blank definition of them.Is there any keyword or method for that. How to do it? Is it possible to do it? In GUI we use adapter classes to achieve it. What for console application?

View Replies View Related

String Declaration - Check A / B Returns False

Jan 30, 2014

I have doubts in string declaration. As I know we can declare string in two ways:

1. String a=new String("Hello");
2. String b="Hello";

What is exact difference between them? Another thing is when I check (a==b) it retuns me false, but when I check a.equals(b) it returns me with true. Why So?

View Replies View Related

Declaration And Initialization On Separate Statement In A Class

Jan 29, 2015

I know that I can declare and initialize an member variable inside a class in a single statement but I can't do them on separate sentence. Why?

For example, I am allowed to do the following,

class A{
int a = 5;
// rest of the class
}

But I am not allowed to do this,
class A{
int a;
a = 5; // it doesn't compile even when the variable 'a' is static
// rest of the class
}

Why java don't allow me to do that?

View Replies View Related

User Event - Global Object Declaration

Mar 10, 2015

I have an application that uses an object that is declared globally for the class. Within a method that is triggered by a user event, it creates a new object and assigns it to this global object declaration. My question is, when the object is overwritten multiple times by the user selecting the button that calls this method, will the older instances be garbage collected or is there still a reference to them? Is there any downfall to this logic and if so what would be a solution.

View Replies View Related

Implementation Of Stack Methods - Declaration Of Stack1 And Stack2

Jun 3, 2014

I have to implement all the stack methods in java such as push, pop empty, not using the ready methods but have to create them and to execute an exercise but is sth wrong with it

public class Stiva {

/** the problem is here how to declare the stack 1 and stack 2 and kreu(head) gjmax(size)*/

int Gjmax;
int array[] = new int[Gjmax];
int kreu;
private Stiva stiva1;
private Stiva stiva2;

[Code] .....

View Replies View Related

Server / Multi Clients

Mar 9, 2014

I have made a server/multi Clients where Clients send Double data but my problem is that the clients send this data to Server only wonce, and the server waits again the connection from client. (I have to run the clients manually each time)what i'm looking: the client should always send data to server. like an infinite loop.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved