Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes struct in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "struct in java" Watch "struct in java" New topic
Author

struct in java

vijayapriya thirumurugan
Greenhorn

Joined: Apr 03, 2009
Posts: 16


hello sir,

can i use struct concept in java. if so tell me about that implementation by means of simple example. i'm waiting..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What about classes ? Is there anything with classes that does not fit what you consider to be a struct ? (I imagine you're talking about C language)


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Christophe Verré wrote: . . . (I imagine you're talking about C language)
He obviously is, yes.

Vijayapriya Thirumurugan: It is important to remember that Java is not C and Java is not an enhancement of C and Java is not C++ and Java is not an enhancement of C++.

Java is a completely different and distinct language.
vijayapriya thirumurugan
Greenhorn

Joined: Apr 03, 2009
Posts: 16
hello sir,


i have seen that struct concept is there in java.

it's syntax is:

static class teststruc extends Struct{
String name;
int number;
}


but i didn't have clear idea about this. that's why i asked. but sir, i'll tell my concept using c language. can you clear that in java.

i have struct in c which contains a field which is of array i.e.,

struct test{
int array;
}
b[10][10];

here i am accessing by means of for loop;
i am storing 1 to b[1][1] .array and 2 to b[1][2], etc again 1 to b[2][1].array etc.
can you give this idea in java

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

i have struct in c which contains a field which is of array i.e.,

struct test{
int array;
}
b[10][10];


You should be able to simulate this data structure, in Java, using classes -- however, since java classes has to be instantiated (unlike structs in c), it will be annoying to setup.

First, you need to define the test "struct"...



Then you need to declare and initialize the b variable, which is a two dimensional array of the test "struct"...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You don't have a struct which is a C/C++ keyword. You have a Struct, which is the name of a Java™ class. You will have to find out where that class came from to get details of it; it's not a J2SE class. Nor is it a struct. But it might be intended to mimic some of the behaviour of a struct.

It is not clear from what you write whether you want an array of Structs, or whether you want a Struct with an int[] field.
vijayapriya thirumurugan
Greenhorn

Joined: Apr 03, 2009
Posts: 16
yes, i need about array of struct only. can you give me example.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
vijayapriya thirumurugan wrote:. . . array of struct . . . give me example.
Henry already has given an example.
vijayapriya thirumurugan
Greenhorn

Joined: Apr 03, 2009
Posts: 16
import java.io.*;
import java.util.*;

public class t {
public static void main(String args[])
{
test b[][] = new test[10][10];
int k=1;
for (int i = 0; i < b.length; i++)
for (int j = 0; j < b[i].length; j++)
{
b[i][j] = new test();
b[i][j].list(new Integer(k));
k++;
}

k=1;
}
}

public class test{
ArrayList li;

public list(int i){
li.add(i);
}
}


i need this output:

0 1 2 3 4 5 6 7 8 9

0 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

1 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

2 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

3 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

4 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

5 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

6 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

7 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

8 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

9 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]


can i get. if not. please give me the code for this output. i'm in running out of time.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

In C++, a struct is more or less the same as a class, but the members are public by default. If you want something similar in Java, you could just create a class with public members:

However, this is not how things are commonly done in Java. If you want something like this, it's better to make a JavaBean out of it, with private member variables and getter and setter methods:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Henry already has given an example.


Not only did I gave an example, I gave you the exact code that you need to make the C struct that you provided work.

The issue here is that you changed that C struct, and didn't change the Java counterpart correctly.



You forgot to instantiate the li reference -- and should get a null pointer exception when you call the add() method.

Henry
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Vijayapriya,

I'd recommend going about this whole thing differently. As opposed to trying to make Java look like C, I'd recommend that you forget about C for the time being and focus on learning Java and the object oriented (OO), concepts that Java uses. Once you have a grasp of OO concepts you can go back to thinking about constructs like "struct" and my guess is that you'll easily come up with OO solutions that are much better than trying to emulate C.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: struct in java